无法以编程方式同时满足约束条件。 Swift iOS

Unable to simultaneously satisfy constraints programmatically. Swift iOS

我正在尝试按一下按钮更改约束条件。

@IBAction func firstButton(_ sender: Any) {
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16).isActive = false
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -46).isActive = true
    someTableView.updateConstraints()
}

@IBAction func secondButton(_ sender: Any) {
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -46).isActive = false
    someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16).isActive = true
    someTableView.updateConstraints()
}

两个约束都处于活动状态后出现错误。他们不会停用:

[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.

[shortened].bottom == [shortened].bottom - 46 (active)>

[shortened].bottom == [shortened].bottom - 16 (active)>

Will attempt to recover by breaking constraint

[shortened].bottom == [shortened].bottom - 16 (active)>

编辑:

这里大家答对了,对我帮助很大。我刚刚接受了带有示例代码的那个。

谢谢大家!

每次点击都会引起新的冲突

var botCon:NSLayoutConstraint!

//

 botCon = someTableView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -16)
 botCon.isActive = true

//

@IBAction func firstButton(_ sender: Any) {
    botCon.constant = -46
    self.view.layoutIfNeeded()
}

@IBAction func secondButton(_ sender: Any) {
    botCon.constant = -16
    self.view.layoutIfNeeded()
}

这里的问题是创建约束的方式。每次在代码中引用约束时,您并不是在引用对象上的实际约束,而是创建一个新约束并最终导致冲突。解决方案是为每个场景在视图控制器中创建 NSLayoutConstraint 对象,然后修改 NSLayoutConstraint .constant 值的值。最后,不要忘记在视图控制器上调用 "layoutIfNeeded()" 函数。