iamarnold iamarnold - 1 year ago 131
Objective-C Question

How to remove constraints from all subviews inside my superview?

I have a UIView that contains multiple UIView subviews that has its own constraints. How do I remove the subviews' constraints?

//only removes the constraints on self.view
[self.view removeConstraints:self.view.constraints];

//I get warning: Incompatible pointer types sending 'NSArray *' to parameter of type 'NSLayoutConstraint'
[self.subview1 removeConstraints:self.subview1.constraints];

Answer Source

Try this code:

for (NSLayoutConstraint *constraint in self.view.constraints) {
    if (constraint.firstItem == self.subview1 || constraint.secondItem == self.subview1) {
        [self.view removeConstraint:constraint];

Basically, this iterates all of the constraints that are assigned to self.view and checks to see whether self.subview1 is involved in the constraint. If so, that constraint gets pulled.