Antoine Antoine - 2 months ago 12x
Swift Question

Correct way to delete a NSLayoutConstraint

In my apps I'm working a lot with constraints, in most cases with animations as well. In certain circumstances I need to remove constraints and add new ones.

As I need to support iOS 7 as well, I'm not able to use the

property, which would else be the solution for me.

The way to remove constraints is to use the
method on a

Is it possible to create a method like


so you don't have to know which view is taking care over the constraint?


I've ended up using the method autoRemove provided by the PureLayout library:

This method finds the commonSuperview using the firstItem or secondItem and removes the constraint from the correct view.

It resulted in this one liner: