removeConstraint() 不删除约束

removeConstraint() not removing constraint

我有一个奇怪的问题。我想在某些条件下更改约束,但 removeConstraint 不起作用。约束没有被删除。

代码如下:

backButton.translatesAutoresizingMaskIntoConstraints = false
view.removeConstraint(constLabelTop)
let constNew = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: backButton, attribute: .CenterY,multiplier: 1, constant: 0)
view.addConstraint(constNew)

约束 constLabelTop 是将 label 的顶部设置在 backButton 上方几个点的约束。为什么不起作用?

新约束与旧约束冲突,backButton 被压扁。

我也试了backButton.removeConstraint,也没用。

试试这个:

backButton.translatesAutoresizingMaskIntoConstraints = false
constLabelTop.active = false
NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: backButton, attribute: .CenterY,multiplier: 1, constant: 0).active = true
self.view.layoutIfNeeded()