自定义UITableViewCell垂直布局约束导致错误

时间:2014-12-10 12:12:27

标签: uitableview ios8 nslayoutconstraint

当我在iPhone上运行我的应用时,我收到以下错误。当我在模拟器中运行它时,我没有。如果我取走-12-|,那么单元格的高度就会缩小到30像素。用户界面打破了。 有人能帮帮我,告诉我为什么吗?

由于

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSLayoutConstraint:0x170285ff0 V:|-(12)-[UIImageView:0x1741ec200]   (Names: '|':UITableViewCellContentView:0x17419c7d0 )>",
"<NSLayoutConstraint:0x170286040 V:[UIImageView:0x1741ec200(200)]>",
"<NSLayoutConstraint:0x170286090 V:[UIImageView:0x1741ec200]-(12)-|   (Names: '|':UITableViewCellContentView:0x17419c7d0 )>",
"<NSLayoutConstraint:0x174881f40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17419c7d0(224)]>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170286090 V:[UIImageView:0x1741ec200]-(12)-|   (Names: '|':UITableViewCellContentView:0x17419c7d0 )>
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.
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
Unable to simultaneously satisfy constraints.

在自定义UITableViewCell中,我按如下方式定义了布局约束:

_imgView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[imageView]-15-|" options:0 metrics:nil views:@{ @"imageView": _imgView }]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[imageView(200)]-12-|" options:0 metrics:metrics views:@{ @"imageView" : _imgView }]];

---编辑---

回应contentView.bounds建议: 在我的UITableViewController中,我实施了以下内容:

_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.estimatedRowHeight = 30.0f;

所以零高度应该不是问题。

1 个答案:

答案 0 :(得分:1)

单元格的初始高度可能小于垂直约束。这会产生初始高度冲突,直到contentView的帧发生变化。

您可以使用以下方法之一解决此问题:

  • 在故事板中增加单元格的高度,使其最初适合内容。

  • 将单元格contentView的边界更改为更大的尺寸:

    self.contentView.bounds = CGRectMake(0, 0, 99999, 99999);

您可以在this auto layout question的答案中找到更多详细信息。

<强>更新

“无法同时满足约束”冲突是在尝试将单元格高度设置为225点的imageView约束和尝试将单元格高度设置为224点的固定高度之间。

您的imageView垂直约束使用224(12 + 200 + 12)个点。 tableView分隔符使用1个点。因此,要满足所有约束,单元高度需要为225点。