iOS 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
}

虽然我想探究tableView HeaderView内部的自动约束,但是没有成功,先把代码这样改吧。

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

推荐阅读更多精彩内容