Ios 是否在TableViewCell中自动布局两个标签?
我对iOS编程相当陌生,可能不太了解视图层次结构,因此无法在我创建的自定义表单元格类中成功获取两个标签,以便正确地自动调整大小。也就是说,Ios 是否在TableViewCell中自动布局两个标签?,ios,objective-c,uitableview,uilabel,autolayout,Ios,Objective C,Uitableview,Uilabel,Autolayout,我对iOS编程相当陌生,可能不太了解视图层次结构,因此无法在我创建的自定义表单元格类中成功获取两个标签,以便正确地自动调整大小。也就是说,“translatesAutoResizengMaskintoConstraints”属性让我有点困惑 我没有为这部分代码使用故事板:我有一个TableViewController,可以在viewDidLoad中创建自己的tableView。在cellforrowatinexpathI初始化我自己的TableViewCell实现中 我遇到的问题是,当我为表视图
“translatesAutoResizengMaskintoConstraints”
属性让我有点困惑
我没有为这部分代码使用故事板:我有一个TableViewController,可以在viewDidLoad
中创建自己的tableView
。在cellforrowatinexpath
I初始化我自己的TableViewCell
实现中
我遇到的问题是,当我为表视图和我创建的UILabels设置“setTranslatesAutoResizezingMacSkintoConstraints”
为NO
,然后添加约束时,我得到以下错误:
"Terminating app due to uncaught exception `'NSInternalInconsistencyException',` reason: 'Auto Layout still required after executing `-layoutSubviews`. UITableView's implementation of `-layoutSubviews` needs to call super.'"
如果我注释掉setTranslatesAutoResizezingMaskintoConstraints
行,我的应用程序将运行,但我会收到以下有关约束的警告:
“无法同时满足约束。可能至少有一个。”
以下列表中的约束之一是您不想要的。请重试
这:(1)查看每个约束,并尝试找出您需要的约束
不要期望;(2)找到添加了不需要的约束或
约束并修复它。(注意:如果您看到
NSAutoresizingMaskLayoutConstraints
如果您不理解,请参阅
查看UIView
属性的文档
翻译自动调整大小GMaskintoConstraints
)”
基本上,我想做的是在这里输入代码,使两个标签彼此对齐,并根据方向/设备调整它们的大小(我将在它们上设置背景色,以便它们看起来“连续”)
有人能帮我解释一下我遗漏了什么吗?提前谢谢
我添加标签的代码是:
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 30.0f)];
self.nameLabel.textColor = [UIColor redColor];
self.nameLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:12.0f];
self.nameLabel.backgroundColor = [UIColor brownColor];
[self.nameLabel setText:@"Test"];
// [self.nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.nameLabel];
首先,您是否在创建self.contentView后向其添加约束 第二,可能您的约束集不足以进行自动布局,它会基于自动调整大小掩码创建自己的约束。尝试为标签添加垂直约束和宽度约束 我对ios编程相当陌生,可能不理解 按我应该的方式查看层次结构,因此未能成功 在我创建的自定义表格单元格类中获取两个标签 正确地自动调整大小。即 “setTranslatesAutoresizingMaskIntoConstraints”属性有一个 有点困惑 嗯,
translatesAutoresizingMaskIntoConstraints
是苹果公司创建的一个属性,它使从自动调整尺寸(弹簧和撑杆)
到自动布局
的转换更容易。比如说,您的视图中有一些自动调整大小的任务
,您只是打开了自动布局
,没有设置任何约束。然后,现有的自动重新设置大小的任务
将转换为约束,从而将视图固定在适当的位置。因此,默认情况下,translatesAutoResizengMaskintoConstraints
属性设置为YES。但是,当您开始添加约束时,在90%的情况下,它们将与通过转换自动ResizingMasks
创建的约束冲突。因此,最好通过设置view.translatesAutoResizezingMaskintoConstraints=NO
在您的代码中,以下可能是造成问题的原因:
- 帧的设置
self.namelab=[[UILabel alloc]init]代码>就足够了
- 设置适当的约束
您的代码:
- 名称标签
- 现在,上面的约束告诉我们,
名称标签
应该是水平的(正如您没有提到的H:或V:)距离容器的“标准”距离(20px),靠近摘要标签
。
但是它的Y位置、宽度和高度呢?
所以我们需要更多的约束
- 摘要标签
- 同样适用于
摘要标签
因此,让我们正确地定义它们:
NSDictionary *viewsDictionary =
NSDictionaryOfVariableBindings(nameLabel, summaryLabel);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel(100)][summaryLabel]-|" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[nameLabel(30)]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[summaryLabel]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel(==summaryLabel)]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:constraints];
[self.view addConstraints:constraints1];
[self.view addConstraints:constraints2];
[self.view addConstraints:constraints3];
现在你的观点看起来不错
在任何时候,若要检查视图是否缺少任何约束,请暂停调试器并在控制台中键入以下命令
po [[UIWindow keyWindow] _autolayoutTrace]
它将显示哪些视图的布局不明确
另外,请记住,在故事板/IB中,如果约束显示为“橙色”,则需要更多约束来定义对象的位置。添加所有必要的约束后,约束颜色变为“蓝色”在
updateConstraints
方法和layoutSubviews
方法中会发生什么?你在调用[super layoutSubviews]
?嗯,根据这一点:你不应该调用setTranslatesAutoResizengMackinToConstraints:不(见文章最后一段)谢谢@Evan,但是我没有覆盖我创建的自定义单元格类中的那些方法,因此不确定错误是什么。@Yohst:我看到了,这是否意味着我不能在我自己的表单元格实现中使用autolayout?我希望我误解了这一点。我不认为您应该为表视图将TranslatesAutoResizezingMaskinToConstraints设置为no,只为标签设置。
NSDictionary *viewsDictionary =
NSDictionaryOfVariableBindings(nameLabel, summaryLabel);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[nameLabel(100)][summaryLabel]-|" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints1 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[nameLabel(30)]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[summaryLabel]" options:0 metrics:nil views:viewsDictionary];
NSArray *constraints3 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel(==summaryLabel)]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:constraints];
[self.view addConstraints:constraints1];
[self.view addConstraints:constraints2];
[self.view addConstraints:constraints3];
po [[UIWindow keyWindow] _autolayoutTrace]