iOS的LayoutConstraint总会莫名出现很多很奇怪的警告,虽然它不影响工程的正常运行,但是每每看到终端输出一大堆的警告提示,对于我们这些有洁癖的人,怎么能够容忍。今天说说我在开发ZHChat时遇到的一个LayoutConstraint问题:UIView-Encapsulated-Layout-Height.
我的Cell布局
没有红色错误和黄色警告提示,一切看起来很正常。
但是一运行,后台一堆LayoutConstraint警告。
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:0x7fe4885ce9d0 V:[ZHCMessagesLabel:0x7fe4885ce6b0(20)]>",
"<NSLayoutConstraint:0x7fe4885cf0d0 V:[ZHCMessagesLabel:0x7fe4885ced70(20)]>",
"<NSLayoutConstraint:0x7fe4885cff50 V:[ZHCMessagesLabel:0x7fe4885cfb90(20)]>",
"<NSLayoutConstraint:0x7fe4885da250 ZHCMessagesCellTextView:0x7fe489088c00.bottom == UIImageView:0x7fe4885d0470.bottom>",
"<NSLayoutConstraint:0x7fe4885da2a0 ZHCMessagesCellTextView:0x7fe489088c00.top == UIImageView:0x7fe4885d0470.top>",
"<NSLayoutConstraint:0x7fe4885da340 V:|-(0)-[UIImageView:0x7fe4885d0470] (Names: '|':UIView:0x7fe4885d0300 )>",
"<NSLayoutConstraint:0x7fe4885da3e0 V:[UIImageView:0x7fe4885d0470]-(0)-| (Names: '|':UIView:0x7fe4885d0300 )>",
"<NSLayoutConstraint:0x7fe4885da9c0 V:[ZHCMessagesLabel:0x7fe4885ce0e0]-(0)-[ZHCMessagesLabel:0x7fe4885ce6b0]>",
"<NSLayoutConstraint:0x7fe4885dab00 ZHCMessagesLabel:0x7fe4885ce0e0.top == UITableViewCellContentView:0x7fe4885cdf30.topMargin - 8>",
"<NSLayoutConstraint:0x7fe4885dabf0 V:[ZHCMessagesLabel:0x7fe4885ce6b0]-(0)-[ZHCMessagesLabel:0x7fe4885ced70]>",
"<NSLayoutConstraint:0x7fe4885dac40 V:[ZHCMessagesLabel:0x7fe4885ced70]-(0)-[UIView:0x7fe4885d0300]>",
"<NSLayoutConstraint:0x7fe4885dac90 UITableViewCellContentView:0x7fe4885cdf30.bottomMargin == ZHCMessagesLabel:0x7fe4885cfb90.bottom - 9>",
"<NSLayoutConstraint:0x7fe4885dace0 V:[UIView:0x7fe4885d0300]-(0)-[ZHCMessagesLabel:0x7fe4885cfb90]>",
"<NSLayoutConstraint:0x7fe4884563f0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fe4885cdf30(55)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe4885da250 ZHCMessagesCellTextView:0x7fe489088c00.bottom == UIImageView:0x7fe4885d0470.bottom>
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.
看警告提示是我的Constraint设置有问题,第一反应去check我的xib中的约束,逐个约束检查后还是没有问题。然后继续看警告提示,UIView-Encapsulated-Layout-Height 说明是高度约束设置不当,而警告中我的Label的Height有变动,查看我Xib中原始的约束是:
看到这里我想大家应该知道是什么原因导致的了,没错就是Equal导致的,修改Equal为Less Than or Equal后警告解决。
这里还有一个快速定位方法就是改变Cell的宽度和高度后如果出现警告,说明约束肯定设置不当!