我有一个奇怪的问题。我想在某些条件下更改约束,但 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()
关于ios - removeConstraint() 不删除约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169823/