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.
Just select the view you want to remove a constraint from and open the size inspector. You should see a list of all the constraints. Select the one you want to delete, and press the delete key.
The way to remove constraints is to use the removeConstraint method on a UIView .
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With