我有一个自定义的UITableViewCell,其布局与我想要的完全一样,但它会引发一个不可满足的约束错误:
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:0x165af570 V:|-(10)-[UIButton:0x167d1bb0'Scan Patient Bracelet Squ...'] (Names: '|':UIView:0x167d1b20 )>",
"<NSLayoutConstraint:0x165af5d0 V:[UIView:0x1652b420]-(0)-| (Names: '|':UIView:0x167d1b20 )>",
"<NSLayoutConstraint:0x165f7de0 UIView:0x1652b420.bottom == UIButton:0x167d1bb0'Scan Patient Bracelet Squ...'.bottom + 10>",
"<NSLayoutConstraint:0x165df2d0 V:[UIView:0x167d1b20]-(0)-| (Names: '|':CMBarcodeScanCell:0x16b88800 )>",
"<NSLayoutConstraint:0x165df300 V:|-(0)-[UIView:0x167d1b20] (Names: '|':CMBarcodeScanCell:0x16b88800 )>",
"<NSLayoutConstraint:0x167d1540 V:[CMBarcodeScanCell:0x16b88800(1)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x15fe1730 UIButton:0x15d38490'Scan Patient Bracelet Squ...'.bottom == UIView:0x15ff5750.bottom - 10>
我无法弄清楚哪个是有问题的约束。约束可以从顶部到底部以及从一侧到另一侧进行跟踪。
任何提示?
修改
经过一些更多的摆弄之后,我将其归结为这三个限制因素:
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:0x1478f570 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.bottom == UITableViewCellContentView:0x1476e930.bottomMargin>",
"<NSLayoutConstraint:0x1478f5a0 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.top == UITableViewCellContentView:0x1476e930.topMargin>",
"<NSLayoutConstraint:0x147aeb00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x1476e930(1)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1478f570 UIButton:0x1475ef00'Scan Patient Bracelet Squ...'.bottom == UITableViewCellContentView:0x1476e930.bottomMargin>
UIView-Encapsulated-Layout-Height为1的第三个约束不在设计中,如下所示:
但现在布局不再正确。按钮文本在单元格内容视图的上方和下方延伸。
问题似乎是根据标签内容大小未计算按钮内容大小。
UIButton扫描按钮的调试器视图:
UIButton文本标签的调试器视图:
我不知道UIButton如何计算其内容大小,但在这种情况下它并不起作用。所以现在我有2个问题,约束冲突和单元格布局。
最终编辑
冲突来自于我正在使用的黑客来调整单元格大小和布局变量按钮文本。它位于heightForRowAtIndexPath:
cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tTblView.bounds), 1);
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];
这确实按照需要进行了单元格布局,但是为contentView高度1引入了约束并在上面创建了冲突。
我还尝试使用CGFLOAT_MAX而不是1,但在运行时也会产生错误消息。
cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tTblView.bounds), CGFLOAT_MAX);
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];
所以我关闭了这个并处理我的自动布局约束,直到我得到正确的布局而不引入这样的黑客代码或导致约束冲突。
答案 0 :(得分:1)
约束可以从顶部到底部以及从一侧到另一侧进行追踪。
这里的问题纯粹是从上到下,你可以通过研究这些约束来轻松看到。我们只考虑这三个限制因素:
V:[UIView:0x167d1b20]-(0)-| (Names: '|':CMBarcodeScanCell:0x16b88800 )
V:|-(0)-[UIView:0x167d1b20] (Names: '|':CMBarcodeScanCell:0x16b88800 )
V:[CMBarcodeScanCell:0x16b88800(1)]
因此,视图0x167的顶部和底部固定在CMBarcodeScanCell的顶部和底部 - 但是你接下来是一个绝对高度约束,它说CMBarcodeScanCell只有1个像素高?这听起来像是一个麻烦的配方。
答案 1 :(得分:0)
如发布问题的最终编辑部分所示,问题不在单元格中,而是在表视图控制器的heightForRowAtIndexPath中的hack代码中。
cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tTblView.bounds), 1);
cell.contentView.bounds = cell.bounds;
[cell layoutIfNeeded];
将bounds height设置为1会引入冲突约束。虽然它使我的单元格布局正确,但这显然不是很好,我只需要找到正确的方法来做到这一点。