iOS的创建NSLayoutConstraint编程为表视图单元格的内容 [英] iOS create NSLayoutConstraint programmatically for table view cell content
问题描述
我想在的cellForRowAtIndexPath一些看法我的单元格内容视图,并为他们的约束,但没有工作的补充。我有这样的事情:
I want to add in the cellForRowAtIndexPath some views to my cell content view and for them constraints but nothing works. I have something like this:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:imageView
attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f];
[cell.contentView addConstraint:constraint];
我应该怎么做呢?
How should I do this?
推荐答案
一对夫妇的意见:
-
您这个特别的约束创作是正确的。很显然,你不能只是设置左手约束,但你需要指定所有将明确地定义单元格的子视图的
架
的约束。例如,定义不仅左侧(或前导)约束,而且顶部,底部,和宽度限制。或定义左约束加的宽度和高度的限制,并指定的垂直y约束。不同的方式许多事情要做,但最关键的是你必须添加所有将明确地定义了框架的约束
所有的子视图的。
Your creation of this particular constraint is correct. Clearly, you can't just set the left constraint, but you need to specify all of the constraints that will unambiguously define the
frame
of the cell's subviews. For example, define not only the left (or leading) constraint, but also the top, bottom, and width constraints. Or define the left constraint plus the the width and height constraints, and specify the vertical y constraint. Lots of different ways to do it, but the key is that you have to add all of the constraints that will unambiguously define theframe
of all of the subviews.
例如,您可能有类似以下内容:
For example, you might have something like the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *customImageView;
UILabel *customLabel;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
customImageView = [[UIImageView alloc] init];
customImageView.translatesAutoresizingMaskIntoConstraints = NO;
customImageView.tag = IMAGEVIEWTAG;
[cell.contentView addSubview:customImageView];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:25.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:30.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:3.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customImageView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-3.0]];
customLabel = [[UILabel alloc] init];
customLabel.translatesAutoresizingMaskIntoConstraints = NO;
customLabel.tag = LABELTAG;
[cell.contentView addSubview:customLabel];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:customImageView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:10.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:-10.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:3.0]];
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:customLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:cell.contentView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-3.0]];
}
else {
customImageView = (id)[cell.contentView viewWithTag:IMAGEVIEWTAG];
customLabel = (id)[cell.contentView viewWithTag:LABELTAG];
}
customImageView.image = ...;
customLabel.text = ...;
return cell;
}
显然,你会经常使用的UITableViewCell
的子类,以方便让你的自定义控件的轨道的过程,但我想保持这个例子简单。
Clearly, you'd frequently use a UITableViewCell
subclass to facilitate the process of keeping track of your custom controls, but I wanted to keep the example simple.
如果您不确定是否限制已经被明确定义,运行应用程序和之后的UI已经presented,暂停应用程序,请在下面的 (LLDB)
提示:
If you're ever unsure of whether the constraints have been defined unambiguously, run the app and after the UI has been presented, pause the app and enter the following at the (lldb)
prompt:
po [[UIWindow keyWindow] _autolayoutTrace]
这将通知您如果有任何意见都含糊不清(即是否有遗漏任何约束)。
This will inform you if any of the views are ambiguously defined (i.e. whether there are any constraints missing).
如果你想看到什么架
是所有的意见,您可以输入在(LLDB)$以下C $ C>提示:
If you want to see what the frame
is for all of the views, you can enter the following at the (lldb)
prompt:
po [[UIWindow keyWindow] recursiveDescription]
请务必注明 translatesAutoresizingMaskIntoConstraints
到 NO
所有子视图,因为我在做上述code样本。
Make sure to specify translatesAutoresizingMaskIntoConstraints
to NO
for all of the subviews, as I did in the above code sample.
虽然你可以使用 constraintWithItem
定义的约束,通常人们会使用 constraintsWithVisualFormat
,因为你可以通常定义约束更简洁的方式。与此code样品对比上面code样品:</ P>
While you can define the constraints using constraintWithItem
, frequently people will use constraintsWithVisualFormat
, as you can often define constraints more concisely that way. Contrast the above code sample with this code sample:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *customImageView;
UILabel *customLabel;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
customImageView = [[UIImageView alloc] init];
customImageView.translatesAutoresizingMaskIntoConstraints = NO;
customImageView.tag = IMAGEVIEWTAG;
[cell.contentView addSubview:customImageView];
customLabel = [[UILabel alloc] init];
customLabel.translatesAutoresizingMaskIntoConstraints = NO;
customLabel.tag = LABELTAG;
[cell.contentView addSubview:customLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(customImageView, customLabel);
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-25-[customImageView(30)]-[customLabel]|" options:0 metrics:nil views:views]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-3-[customImageView]-3-|" options:0 metrics:nil views:views]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-3-[customLabel]-3-|" options:0 metrics:nil views:views]];
}
else {
customImageView = (id)[cell.contentView viewWithTag:IMAGEVIEWTAG];
customLabel = (id)[cell.contentView viewWithTag:LABELTAG];
}
customImageView.image = ...;
customLabel.text = ...;
return cell;
}
这篇关于iOS的创建NSLayoutConstraint编程为表视图单元格的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!