I have a UIView that fits on the screen with a few limitations. Some of the restrictions belong to the superwin, others belong to other ancestors (for example, perhaps, the view property of the UIViewController).
I want to remove all of these old restrictions and put it somewhere new, using the new restrictions.
How can I do this without creating an IBOutlet for each individual constraint and donβt remember which representation belongs to the specified constraint?
To develop, a naive approach would be to create an IBOutlets group for each of the constraints and then include a call code, for example:
[viewA removeConstraint:self.myViewsLeftConstraint]; [viewB removeConstraint:self.myViewsTopConstraint]; [viewB removeConstraint:self.myViewsBottomConstraint]; [self.view removeConstraint:self.myViewsRightConstraint];
The problem with this code is that even in the simplest case, I would need to create 2 IBOutlets. For complex layouts, this can easily achieve the 4 or 8 required IBOutlets. In addition, I will need to make sure that my call to remove the constraint is called in the correct view. For example, imagine myViewsLeftConstraint belongs to viewA . If I accidentally called [self.view removeConstraint:self.myViewsLeftConstraint] , nothing would happen.
Note. The constraintsAffectingLayoutForAxis method looks promising, but is for debugging purposes only.
Update:. Many of the answers I get deal with self.constraints , self.superview.constraints or some variations. These solutions will not work, because these methods return only restrictions that belong to the view, and not those that affect the view.
To clarify the problem with these solutions, consider this view hierarchy:
Now imagine that we create the following constraints and always attach them to their closest common ancestor:
- C0: Me: the same top as the Son (belongs to me)
- C1: Me: width = 100 (belongs to me)
- C2: Me: same height as brother (belongs to father)
- C3: Me: same top as uncle (belonging to grandfather).
- C4: Me: the same as grandfather (belonging to grandfather).
- C5: Brother: the same as the Father (belongs to the father)
- C6: Uncle: the same as grandfather (belonging to grandfather).
- C7: Son: the same as the Daughter (belongs to me).
Now imagine that we want to remove all restrictions affecting Me . Any correct solution should remove [C0,C1,C2,C3,C4] and nothing else.
If I use self.constraints (where self is Me), I will get [C0,C1,C7] , as these are the only restrictions Me has. Obviously, this would not be enough to remove it, since it is missing [C2,C3,C4] . In addition, it unnecessarily removes C7 .
If I use self.superview.constraints (where self is Me), I will get [C2,C5] , since these are restrictions that belong to the Father. Obviously, we cannot remove all this, since C5 completely unrelated to Me .
If I use grandfather.constraints , I will get [C3,C4,C6] . Again, we cannot remove all of them, since C6 must remain intact.
The brute force approach is to loop over each of the ancestors of the ancestors (including yourself) and see if firstItem or secondItem the view itself; if so, remove this restriction. This will lead to the correct solution, returning [C0,C1,C2,C3,C4] and only these restrictions.
However, I hope there is a more elegant solution than having to go through the entire list of ancestors.