132

The question I have is simple but I couldn't find any information in the documentation.

What happens with layout constraints when a view is removed from the view hierarchy (or moved to another view)?

For example, let's have container C with subviews A and B. Container C holds some constraints. Then we call [A removeFromSuperview]. What happens with the constraints for A?

What then happens if we add A to C again?

6 Answers 6

131

The constraints are removed. If you add A again, you will have to make new constraints for it, or if you save the constraints before you remove A, you can add them back. When I do something like this, I save the constraints like this for a view called view1:

self.portraitConstraints = [NSMutableArray new];
for (NSLayoutConstraint *con in self.view.constraints) {
    if (con.firstItem == self.view1 || con.secondItem == self.view1) {
       [self.portraitConstraints addObject:con];
    }
}
11
  • 7
    Can be this fact found somewhere in the documentation? I believe you but it seems strange the fact isn't mentioned anywhere.
    – Sulthan
    Commented Sep 4, 2013 at 15:53
  • 2
    It's common sense that the constraints are removed along with the view really. If they weren't how would the layout system be able to evaluate them in a future pass? Commented Sep 4, 2013 at 16:13
  • 6
    For as obvious as this answer seems like it would be, it was still tremendously helpful!
    – race_carr
    Commented Mar 21, 2014 at 17:46
  • 1
    @pnollet, I don't know why the poster in that question did what he did. I've logged this to verify, and when I remove a subview, and check the constraints on the superview, those constraints that pertained to the removed subview are gone.
    – rdelmar
    Commented Apr 22, 2014 at 15:18
  • 4
    @Firo, I'm sure they're supposed to be removed, so if they're not in iOS 6, it's probably a bug. BTW, the doc for the removeFromSuperview method says, "Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing".
    – rdelmar
    Commented Aug 25, 2014 at 22:42
52

Since I had this question too, I checked the Apple Docs just for kicks, and it turns out that it is documented that the constraints are removed.

The documentation for the UIView removeFromSuperview method states:

Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.

I'm not sure if this was documented last year when the original question was posted, but I just thought I'd share this information in case anyone needed it...

1
  • Awesome thanks for adding this straight from the doc
    – Esko918
    Commented Dec 1, 2015 at 20:46
3

Be aware though, that if you have two independent parent views A and B, and a subview C, where C is currently a subview of A, with appropriate constraints, that calling [B addSubview:C] will NOT clear any constraints relating to A and C, and auto layout will start throwing exceptions, because those constraints no longer relate to views in the same hierarchy.

You will need to call [C removeFromSuperview] explicitly to remove the constraints, before adding C to B.

This is true on Mac OS X - I haven't checked iOS

1
  • Interesting! Where did you get this information? The docs for addSubview don't mention this behaviour. I'm asking because I currently have a sporadic exception, when removing a view controller, that states 'Unable to install constraint on view' -- something I'm not doing.
    – JimmyB
    Commented Apr 20, 2017 at 14:38
2

The constraints are also removed when you [A removeFromSuperview]

They are forgotten and adding A to C again adds no constraints.

0

They are removed too, you can do a simple test. Pick up a view SUBVIEW and create costraints that constraint SUBVIEW to follow its superview resizing (like attched to to superview edges). To do that you add SUBVIEW as a subview to this CONTAINERVIEW and add as constraints something like that:
V:|-[SUBVIEW]-|
H:|-[SUBVIEW]-|
These constraints should be added to SUBVIEW superview, thus CONTAINERVIEW.
If you remove SUBVIEW by simply checking all the CONTAINERVIEW constraints you could see that two aren't around anymore.

-1

This question also can be proved by interface builder. When drag and drop a UIView on the ViewController add constraints then remove the UIView, you can see the blue constraints disappear.

1
  • 6
    Storyboard proves nothing about how runtime behavior will occur. Storyboard can do whatever it wants!
    – mxcl
    Commented Sep 24, 2017 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.