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. 
(
    "",
    "",
    "",
    ""
)

Will attempt to recover by breaking constraint 


Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  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
}

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

你可能感兴趣的:(iOS UITableView设置tableHeaderView时发生约束错误)