Lawrence413
Lawrence413

Reputation: 2016

removeConstraint() not removing constraint

I have a weird problem. I want to change a constraint in certain conditions, but removeConstraint doesn't work. The constraint doesn't get removed.

Here's the code:

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)

The constraint constLabelTop is a constraint which sets the top of the label a few points above the backButton. Why doesn't it work?

The new constraint clashes with the old one and the backButton gets squashed.

I tried backButton.removeConstraint too and didn't work either.

Upvotes: 6

Views: 1708

Answers (1)

Twitter khuong291
Twitter khuong291

Reputation: 11672

Try this:

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()

Upvotes: 5

Related Questions