带有 2 个标签的单元格总是 'unable to simultaneously satisfy constraints'

Cell with 2 labels always is 'unable to simultaneously satisfy constraints'

我有一个带有 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'。

Edit:作为旁注,我不想限制 body 的高度,因为它是一个高度因标签而异的标签关于其中的文字量。

编辑 2:使用@siburb 提供的解决方案警告消失。但是一个新问题仍然存在,如上面的屏幕显示主体所示,是一个大的 UILabel,我已将 lines 属性 设置为等于 0 因为这应该允许标签变化它的大小基于文本输入的数量。当我 运行 程序时(在下面的屏幕截图中)我只能看到 sayer UILabel。正文标签中有文字(我测试过)所以这不是问题。

并且在我的 RecentTableViewController.h(此 table 视图的文件)中我包含了方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

它应该动态调整 UILabel 的大小,我之前在这个确切的设置中让它工作但是我的约束搞砸了而不是它不工作。

编辑 3:这是在 编辑 @siburb 回复的部分。

您需要减少其中一个约束的 "priority"。

尝试将 "body" 的底部约束的优先级降低到 900 - 这意味着 "sayer" 的高度约束不会被破坏。

此外,将 "body" UILabel 的 "Vertical Content Hugging Priority" 减少到 1 可能会有所帮助 - 这意味着 "body" UILabel 不会那么热衷于坚持高度"intrinsic content size"(大小由标签内容决定)。

编辑: 要解决第二个问题,您可能需要增加 "body" 用户界面标签。例如,将其增加到 999。

您也不需要 heightForRowAtIndexPath: 方法。只需在您的 viewDidLoad: 方法中包含如下内容:

self.tableView.estimatedRowHeight = 100; self.tableView.rowHeight = UITableViewAutomaticDimension;