What happens with constraints when a view is removed
IosAutolayoutNslayoutconstraintIos Problem Overview
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?
Ios Solutions
Solution 1 - Ios
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];
}
}
Solution 2 - Ios
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...
Solution 3 - Ios
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
Solution 4 - Ios
The constraints are also removed when you [A removeFromSuperview]
They are forgotten and adding A to C again adds no constraints.
Solution 5 - Ios
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.
Solution 6 - Ios
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.