Skip to content

Commit

Permalink
Update behavior of recursive constraint removal method to exclude int…
Browse files Browse the repository at this point in the history
…rinsic content size constraints

Expose a new method which allows ALL constraints (including intrinsic content size constraints) to be removed.
  • Loading branch information
smileyborg committed Sep 2, 2013
1 parent 5d5252e commit 4ada54c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Source/UIView+AutoLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ typedef void(^ALConstraintsBlock)(void); // a block of method calls to the UI
/** Removes the given constraints from the views they have been added to. */
+ (void)removeConstraints:(NSArray *)constraints;

/** Recursively removes all constraints from the view and its subviews. */
/** Recursively removes all layout constraints from the view and its subviews.
NOTE: This method preserves low-priority intrinsic content size constraints, which you usually do not want to remove. */
- (void)removeAllConstraintsFromViewAndSubviews;

/** Recursively removes all constraints from the view and its subviews, optionally including intrinsic content size constraints.
WARNING: Intrinsic content size constraints are auto-generated lower priority constraints, and you usually do not want to remove these. */
- (void)removeAllConstraintsFromViewAndSubviewsIncludingIntrinsicContentSizeConstraints:(BOOL)shouldRemoveIntrinsicContentSizeConstraints;


/** Centers the view in its superview. */
- (NSArray *)autoCenterInSuperview;
Expand Down
26 changes: 23 additions & 3 deletions Source/UIView+AutoLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,33 @@ + (void)removeConstraints:(NSArray *)constraints
}

/**
Recursively removes all constraints from the view and its subviews.
Recursively removes all layout constraints from the view and its subviews.
NOTE: This method preserves low-priority intrinsic content size constraints, which you usually do not want to remove.
*/
- (void)removeAllConstraintsFromViewAndSubviews
{
[UIView removeConstraints:self.constraints];
[self removeAllConstraintsFromViewAndSubviewsIncludingIntrinsicContentSizeConstraints:NO];
}

/**
Recursively removes all constraints from the view and its subviews, optionally including intrinsic content size constraints.
WARNING: Intrinsic content size constraints are auto-generated lower priority constraints that attempt to keep a view at its
intrinsic content size (by hugging its content & resisting compression), and you usually do not want to remove these.
@param shouldRemoveIntrinsicContentSizeConstraints Whether intrinsic content size constraints should be removed or skipped.
*/
- (void)removeAllConstraintsFromViewAndSubviewsIncludingIntrinsicContentSizeConstraints:(BOOL)shouldRemoveIntrinsicContentSizeConstraints
{
NSMutableArray *constraintsToRemove = [NSMutableArray new];
for (NSLayoutConstraint *constraint in self.constraints) {
BOOL isIntrinsicContentSizeConstraint = [NSStringFromClass([constraint class]) isEqualToString:@"NSContentSizeLayoutConstraint"];
if (shouldRemoveIntrinsicContentSizeConstraints || !isIntrinsicContentSizeConstraint) {
[constraintsToRemove addObject:constraint];
}
}
[UIView removeConstraints:constraintsToRemove];
for (UIView *subview in self.subviews) {
[subview removeAllConstraintsFromViewAndSubviews];
[subview removeAllConstraintsFromViewAndSubviewsIncludingIntrinsicContentSizeConstraints:shouldRemoveIntrinsicContentSizeConstraints];
}
}

Expand Down

0 comments on commit 4ada54c

Please sign in to comment.