Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all constraints affecting a UIView

I have a UIView which is placed on the screen via several constraints. Some of the constraints are owned by the superview, others are owned by other ancestors (e.g. perhaps the view property of a UIViewController).

I want to remove all of these old constraints, and place it somewhere new using new constraints.

How can I do this without creating an IBOutlet for every single constraint and having to remember which view owns said constraint?

To elaborate, the naive approach would be to create a bunch of IBOutlets for each of the constraints, and would then involve calling code such as:

[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 could easily reach 4 or 8 required IBOutlets. Furthermore, I would need to ensure that my call to remove the constraint is being called on the proper view. For example, imagine that myViewsLeftConstraint is owned by viewA. If I were to accidentally call [self.view removeConstraint:self.myViewsLeftConstraint], nothing would happen.

Note: The method constraintsAffectingLayoutForAxis looks promising, but is intended for debugging purposes only.


Update: Many of the answers I am receiving deal with self.constraints, self.superview.constraints, or some variant of those. These solutions won't work since those methods return only the constraints owned by the view, not the ones affecting the view.

To clarify the problem with these solutions, consider this view hierarchy:

  • Grandfather
    • Father
      • Me
        • Son
        • Daughter
      • Brother
    • Uncle

Now imagine we create the following constraints, and always attach them to their nearest common ancestor:

  • C0: Me: same top as Son (owned by Me)
  • C1: Me: width = 100 (owned by Me)
  • C2: Me: same height as Brother (owned by Father)
  • C3: Me: same top as Uncle (owned by Grandfather)
  • C4: Me: same left as Grandfather (owned by Grandfather)
  • C5: Brother: same left as Father (owned by Father)
  • C6: Uncle: same left as Grandfather (owned by Grandfather)
  • C7: Son: same left as Daughter (owned by Me)

Now imagine we want to remove all constraints affecting Me. Any proper 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], since those are the only constraints owned by Me. Obviously it wouldn't be enough to remove this since it is missing [C2,C3,C4]. Furthermore, it is removing C7 unnecessarily.

If I use self.superview.constraints (where self is Me), I will get [C2,C5], since those are the constraints owned by Father. Obviously we cannot remove all these since C5 is completely unrelated to Me.

If I use grandfather.constraints, I will get [C3,C4,C6]. Again, we cannot remove all of these since C6 should remain intact.

The brute force approach is to loop over each of the view's ancestors (including itself), and seeing if firstItem or secondItem are the view itself; if so, remove that constraint. This will lead to a correct solution, returning [C0,C1,C2,C3,C4], and only those constraints.

However, I'm hoping there is a more elegant solution than having to loop through the entire list of ancestors.

like image 587
Senseful Avatar asked Jun 25 '14 21:06

Senseful


People also ask

How do I delete a constraint in Swift?

Just select the view you want to remove a constraint from and open the size inspector. You should see a list of all the constraints. Select the one you want to delete, and press the delete key.

How should I delete all my constraints in Xcode?

To remove constraints for a single view you select the view and hit the triangle button in the bottom right hand corner... And hit "Clear Constraints" under the "Selected Views" part. To clear constraints for an entire View Controller do the same but hit "Clear Constraints" under the "All Views in ..." part.


2 Answers

This approach worked for me:

@interface UIView (RemoveConstraints)  - (void)removeAllConstraints;  @end   @implementation UIView (RemoveConstraints)  - (void)removeAllConstraints {     UIView *superview = self.superview;     while (superview != nil) {         for (NSLayoutConstraint *c in superview.constraints) {             if (c.firstItem == self || c.secondItem == self) {                 [superview removeConstraint:c];             }         }         superview = superview.superview;     }      [self removeConstraints:self.constraints];     self.translatesAutoresizingMaskIntoConstraints = YES; }  @end 

After it's done executing your view remains where it was because it creates autoresizing constraints. When I don't do this the view usually disappears. Additionally, it doesn't just remove constraints from superview but traversing all the way up as there may be constraints affecting it in ancestor views.


Swift 4 Version

extension UIView {          public func removeAllConstraints() {         var _superview = self.superview                  while let superview = _superview {             for constraint in superview.constraints {                                  if let first = constraint.firstItem as? UIView, first == self {                     superview.removeConstraint(constraint)                 }                                  if let second = constraint.secondItem as? UIView, second == self {                     superview.removeConstraint(constraint)                 }             }                          _superview = superview.superview         }                  self.removeConstraints(self.constraints)         self.translatesAutoresizingMaskIntoConstraints = true     } } 
like image 180
marchinram Avatar answered Oct 16 '22 01:10

marchinram


The only solution I have found so far is to remove the view from its superview:

[view removeFromSuperview] 

This looks like it removes all constraints affecting its layout and is ready to be added to a superview and have new constraints attached. However, it will incorrectly remove any subviews from the hierarchy as well, and get rid of [C7] incorrectly.

like image 35
Senseful Avatar answered Oct 16 '22 01:10

Senseful