iOS UITableView设置tableHeaderView时发生约束错误

今天为UITableView设置了tableHeaderView,都是正常的约束会,然而出现了以下问题:

[LayoutConstraints] 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. 
(
    "<SnapKit.LayoutConstraint:0x6000019e1920@XXXHeaderView.swift#173 UIImageView:0x7fccbc420240.left == XXXHeaderView:0x7fccbc658190.left + 15.0>",
    "<SnapKit.LayoutConstraint:0x6000019e1aa0@XXXHeaderView.swift#180 UILabel:0x7fccbc420470.left == UIImageView:0x7fccbc420240.right + 10.0>",
    "<SnapKit.LayoutConstraint:0x6000019e1b00@XXXHeaderView.swift#181 UILabel:0x7fccbc420470.right == XXXHeaderView:0x7fccbc658190.right - 15.0>",
    "<NSLayoutConstraint:0x600001e82260 'UIView-Encapsulated-Layout-Width' XXXHeaderView:0x7fccbc658190.width == 0   (active)>"
)

Will attempt to recover by breaking constraint 
<SnapKit.LayoutConstraint:0x6000019e1aa0@XXXHeaderView.swift#180 UILabel:0x7fccbc420470.left == UIImageView:0x7fccbc420240.right + 10.0>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

这其实是UITableView为你的自定义View自动添加了其他约束,然而它添加的约束和你添加的约束的优先级是相同的。如果要使你设置的约束有效,就将你设置的约束的优先级提高。
我用的是 SnapKit 约束的,设置如下:

titleLabel.snp.makeConstraints { (make) in
      make.top.equalTo(imgView.snp.top)
      make.left.equalTo(imgView.snp.right).offset(10)
      make.right.equalTo(self.snp.right).offset(-15).constraint.layoutConstraints.first?.priority = UILayoutPriority.defaultHigh
}

如果有更好的解决办法请留言评论 😊

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

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,026评论 3 119
  • 今年我大四 生活的精巧就在于,每一个看似前途无望的背后,都蕴含着转机。在每次痛苦地思索,辗转反侧地推敲以后,于某个...
    跟着内心走的小鸭子阅读 286评论 0 0
  • 去年,由许鞍华执导,李樯编剧和监制的电影《黄金时代》上映,汤唯,冯绍峰,王志文,朱亚文等主演。影片以民国时代为大背...
    某人李下阅读 2,021评论 6 24