UITableViewCell LayoutConstraint (UIView-Encapsulated-Layout-Height)

iOS的LayoutConstraint总会莫名出现很多很奇怪的警告,虽然它不影响工程的正常运行,但是每每看到终端输出一大堆的警告提示,对于我们这些有洁癖的人,怎么能够容忍。今天说说我在开发ZHChat时遇到的一个LayoutConstraint问题:UIView-Encapsulated-Layout-Height.

我的Cell布局

屏幕快照 2016-09-02 11.07.19.png

没有红色错误和黄色警告提示,一切看起来很正常。
但是一运行,后台一堆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中原始的约束是:

屏幕快照 2016-09-02 11.17.24.png

看到这里我想大家应该知道是什么原因导致的了,没错就是Equal导致的,修改Equal为Less Than or Equal后警告解决。

这里还有一个快速定位方法就是改变Cell的宽度和高度后如果出现警告,说明约束肯定设置不当!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 0、前言 一、Auto Layout前世今生 二、Auto Layout基础知识 1.Auto Layout...
    浮游lb阅读 25,032评论 3 90
  • 7、不使用IB是,下面这样做有什么问题? 6、请说说Layer和View的关系,以及你是如何使用它们的。 1.首先...
    AlanGe阅读 736评论 0 1
  • 原文地址: RAYWENDERLICH 说明:英文水平有限,主要是为了巩固学到的知识,也能帮别人快速上手,节约时间...
    Hi川阅读 4,298评论 1 6
  • 引言 iPhone5使屏幕从3.5"增大到了4.0",与之对应的iOS6也就引入了auto layout,auto...
    二亮子阅读 6,475评论 6 24
  • KVC的本质就是 (键值编码) 定义: 在对象创建完成之后,动态(牵扯到运行时)的给对象的属性赋值 KVC,即是指...
    平凡的心阅读 4,807评论 0 2