Correct way to delete a NSLayoutConstraint Correct way to delete a NSLayoutConstraint xcode xcode

Correct way to delete a NSLayoutConstraint


What I do is create arrays of the constraints that I wish to be able to add/remove and simply use the following:

@property NSMutableArray *newConstraints;

Fill up the newConstraints

iOS7 and iOS8:

[self.viewToChange addConstraints:self.newConstraints];[self.viewToChange removeConstraints:self.newConstraints];

or iOS8 only, use the new mechanism

[NSLayoutConstraint activateConstraints:self.newConstraints];[NSLayoutConstraint deactivateConstraints:self.newConstraints];

With this you can apply a set, remove the set and apply a new set.

You can also create the initial list from the storyboard set if you can identify which constraints are which.


I've ended up using the method autoRemove provided by the PureLayout library: https://github.com/smileyborg/PureLayout

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

It resulted in this one liner:

containerTopConstraint.autoRemove()


No, not that I'm aware of. The automatic managing of the host view only came in iOS8.

An ugly implementation could loop over all constraints of all views to finde the view where it is on.

But normally it shouldn't be to hard to manage the constrains in a way that you always know on which view they are defined.