I read many topics about how to remove constraints that were added through the storyboard, drag outlet and then remove etc. but how I can remove constraints that were added programmatically? For example
firstView.topAnchor.constraints(equalTo: secondView.bottomAnchor, constant: 15).isActive = true
how can I deactivate it and then enable it again if needed. Maybe it should be something like?
firstView.removeConstraint(firstView.topAnchor.constraints(equalTo: secondView.bottomAnchor))
You should assign your constraint to a property in your ViewController
. And then set .isActive
to false
instead of true.
Your code should look like this:
let myConstraint = firstView.topAnchor.constraint(equalTo: secondView.bottomAnchor, constant: 15)
Now, to activate it:
myConstraint.isActive = true
And to disable it:
myConstraint.isActive = false