【发布时间】:2015-11-30 00:06:55
【问题描述】:
我有一个带有 2 个简单标签的单元格,我在过去 6 个小时内一直在工作以使其正常工作(在此之前它工作过,并决定修补它并丢失了我的工作版本)。我总是收到Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. 警告。这是我在使用推荐的约束时收到的警告:
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x7f8fe8737520 V:[UILabel:0x7f8fe873d5a0'Label'(26)]>",
"<NSLayoutConstraint:0x7f8fe8738ea0 UILabel:0x7f8fe873d7a0'Label'.top == UITableViewCellContentView:0x7f8fe873cf00.topMargin>",
"<NSLayoutConstraint:0x7f8fe8739000 UILabel:0x7f8fe873d5a0'Label'.bottom == UITableViewCellContentView:0x7f8fe873cf00.bottomMargin>",
"<NSLayoutConstraint:0x7f8fe8739110 V:[UILabel:0x7f8fe873d7a0'Label']-(NSSpace(8))-[UILabel:0x7f8fe873d5a0'Label']>",
"<NSLayoutConstraint:0x7f8fe8743ac0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f8fe873cf00(43.6667)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f8fe8737520 V:[UILabel:0x7f8fe873d5a0'Label'(26)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
这是我在故事板中的原型单元(如果您现在还没有弄清楚,这是自定义的):
右下角的小标签是“Sayer”,较大的标签是“Body”。
编辑:作为旁注,我不想限制 body 的高度,因为它是一个标签,其高度取决于其中的文本数量。
编辑 2:使用@siburb 提供的解决方案,警告消失了。但是仍然存在一个新问题,如上面的屏幕显示主体是一个大的 UILabel,我已将其 lines 属性设置为等于 0,因为这应该允许标签根据文本量改变其大小输入。当我运行程序时(在下面的屏幕截图中)我只能看到说 UILabel。正文标签中有文本(我测试过)所以这不是问题。
在我的RecentTableViewController.h(此表视图的文件)中,我包含了该方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
它应该动态调整 UILabel 的大小,我之前在这个精确的设置中让它工作,但我的约束搞砸了,并不是它不起作用。
编辑 3:这是在编辑 @siburb 响应的部分。
【问题讨论】:
-
您已将
Sayer的高度限制为 26,但它的高度也由顶部/底部约束设置,因此存在冲突。自动布局打破了 height=26 约束以解决冲突。要么删除高度约束或顶部或底部约束,要么设置约束的优先级,以便自动布局知道哪个更重要 -
@brettf - 我在下面的答案中添加了更多信息。希望这会有所帮助。
标签: ios objective-c autolayout uistoryboard