removeConstraint()无法正常工作

Law*_*413 5 ios nslayoutconstraint swift

我有一个奇怪的问题。我想在某些条件下更改约束,但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)
Run Code Online (Sandbox Code Playgroud)

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

新约束与旧约束发生冲突,并且backButton被压缩。

我也尝试backButton.removeConstraint过,也没有用。

Khu*_*ong 5

尝试这个:

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()
Run Code Online (Sandbox Code Playgroud)