【发布时间】:2013-10-08 14:23:59
【问题描述】:
我正在以编程方式使用自动布局约束来布局我的自定义 UITableView 单元格,并且我在 tableView:heightForRowAtIndexPath: 中正确定义了单元格大小
它在 iOS6 上运行良好,在 iOS7 上也看起来正常
但是当我在 iOS7 上运行应用程序时,我在控制台中看到的消息是这样的:
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] 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:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50] (Names: '|':UITableViewCellContentView:0xd93e850 )>",
"<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",
"<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>",
"<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>",
"<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-| (Names: '|':UITableViewCellContentView:0xd93e850 )>",
"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>
而且确实在那个列表中有一个我不想要的约束:
"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
我无法将contentView 的translatesAutoresizingMaskIntoConstraints 属性设置为NO =>它会弄乱整个单元格。
44 是默认单元格高度,但我在表格视图委托中定义了我的自定义高度,那么为什么单元格 contentView 有这个约束?什么可能导致这种情况?
在 iOS6 中没有发生这种情况,在 iOS6 和 iOS7 上一切看起来都很好。
我的代码很大,所以我不会在此处发布,但如果您需要,请随时索取 pastebin。
在单元初始化时指定我的操作方式:
- 我创建了所有标签、按钮等
- 我将他们的
translatesAutoresizingMaskIntoConstraints属性设置为 NO - 我将它们添加为单元格的
contentView的子视图 - 我在
contentView上添加了约束
我也很想了解为什么这种情况只发生在 iOS7 上。
【问题讨论】:
-
您的自定义单元格高度设置为多少?
-
我的自定义单元格高度设置为 90
-
一位同事昨天遇到了同样的问题,但默认的 width 为 320(对于 iPad 应用)。
-
这里有一个示例项目演示了同样的问题:github.com/Alex311/TableCellWithAutoLayout 以下是我对它的一些观察:github.com/Alex311/TableCellWithAutoLayout/commit/…
-
我遇到了同样的错误,但这是因为我从未初始化用于查找自定义单元格高度的额外原型单元格。
标签: ios ios7 uitableview autolayout nslayoutconstraint