What happens to restrictions when deleting a view

My question is simple, but I could not find any information in the documentation.

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

For example, let container C with subviews A and B Container C contains some restrictions. Then we call [A removeFromSuperview] . What happens to the restrictions for A ?

What happens if we add A to C again?

+82
ios autolayout nslayoutconstraint
Sep 04 '13 at 15:31
source share
6 answers

Limitations are removed. If you add A again, you will have to create new restrictions for it, or if you keep the restrictions before deleting A, you can add them back. When I do something like this, I keep restrictions similar to this for the view named 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]; } } 
+94
Sep 04 '13 at 15:44
source share

Since I also had this question, I checked Apple Docs only for strokes, and it turns out that it is documented that the restrictions are removed.

documentation for the UIView removeFromSuperview removal method:

Calling this method removes any restrictions that apply to the view you are deleting or referring to any view in the subtree of the view you are deleting.

I'm not sure if this was recorded last year when the original question was sent, but I just thought that I would share this information if someone needs it ...

+30
Jul 07 '15 at 10:25
source share

Keep in mind that if you have two independent parent views A and B and subview C, where C is currently a subordinate of A, with corresponding restrictions, then calling [B addSubview: C] will NOT clear any restrictions related to A and C, and auto-linking will start throwing exceptions, since these restrictions are no longer associated with views in the same hierarchy.

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

This is true on Mac OS X - I have not tested iOS

+3
Oct 10 '16 at 20:24
source share

Constraints are also removed if you [A removeFromSuperview]

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

+2
Sep 04 '13 at 15:44
source share

They are also removed, you can do a simple test. Take the SUBVIEW view and create price constraints that restrict SUBVIEW to keep track of how the supervisor resizes (for example, based on the boundaries of the supervisor). To do this, you add SUBVIEW as a routine to this CONTAINERVIEW and add something like this as a constraint:
V: | - [subspecies] - |
H: | - [subspecies] - |
These restrictions must be added to the SUBVIEW superview, thus CONTAINERVIEW.
If you remove SUBVIEW just by checking all the CONTAINERVIEW constraints, you can see that they are no more.

+1
Sep 04 '13 at 15:45
source share

This question can also be proved by the interface constructor. When you drag and drop a UIView into the constraints of the ViewController add, then remove the UIView , you will see that the blue constraints disappear.

0
Apr 12 '17 at
source share



All Articles