修改约束的常量会在控制台中生成错误

时间:2017-05-20 10:46:59

标签: ios autolayout

我有tableView连接到superview的边缘。

    tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
    tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
    tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
    tableViewBottomConstraint = tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0)
    tableViewBottomConstraint?.isActive = true

在某些情况下,我需要在viewDidAppear中修改tableView的底部约束,我喜欢这样做

tableViewBottomConstraint?.isActive = false
tableViewBottomConstraint?.constant = -48
tableViewBottomConstraint?.isActive = true

但这会产生错误

2017-05-20 13:41:55.347456+0300 fooder[74115:9014905] [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.  
(
"<NSLayoutConstraint:0x608000096530 UITableView:0x7fe54f83b600.bottom == UIView:0x7fe54f52b050.bottom   (active)>",
"<NSLayoutConstraint:0x600000097660 UITableView:0x7fe54f83b600.bottom == UIView:0x7fe54f52b050.bottom - 48   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000096530 UITableView:0x7fe54f83b600.bottom ==     UIView:0x7fe54f52b050.bottom   (active)>

我做错了什么?

1 个答案:

答案 0 :(得分:0)

通过查看NSLayoutConstrainttableViewBottom<NSLayoutConstraint:0x608000096530似乎有NSLayoutConstraint:0x600000097660个重复。

确保检查您的代码是否重复约束将解决问题。