Skip to content

Commit

Permalink
Add methods to set implicit constraint priorities within a constraint…
Browse files Browse the repository at this point in the history
…s block

Two new API methods allow the priority of the content compression
resistance & content hugging implicit constraints to be set from within
the block passed into the method +[UIView
autoSetPriority:forConstraints:]
  • Loading branch information
smileyborg committed Mar 2, 2014
1 parent 94c4cb4 commit fa7ad11
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
1 change: 1 addition & 0 deletions Example/Example/ALViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ - (void)setupDemo4
Demonstrates:
- Everything from Demo 4, and...
- Using leading/trailing edge attributes instead of left/right
(Change language from English to Arabic to see the difference.)
*/
- (void)setupDemo5
{
Expand Down
13 changes: 12 additions & 1 deletion Source/UIView+AutoLayout.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UIView+AutoLayout.h
// v1.1.0
// v1.2.0
// https://github.com/smileyborg/UIView-AutoLayout
//
// Copyright (c) 2012 Richard Turton
Expand Down Expand Up @@ -179,6 +179,17 @@ typedef void(^ALConstraintsBlock)(void); // a block of method calls to the UI
- (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)size relation:(NSLayoutRelation)relation;


#pragma mark Set Content Compression Resistance & Hugging

/** Sets the priority of content compression resistance for an axis.
NOTE: This method must only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:] */
- (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis;

/** Sets the priority of content hugging for an axis.
NOTE: This method must only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:] */
- (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis;


#pragma mark Constrain Any Attributes

/** Constrains an attribute (any ALEdge, ALAxis, or ALDimension) of the view to a given attribute of another view. */
Expand Down
83 changes: 78 additions & 5 deletions Source/UIView+AutoLayout.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// UIView+AutoLayout.m
// v1.1.0
// v1.2.0
// https://github.com/smileyborg/UIView-AutoLayout
//
// Copyright (c) 2012 Richard Turton
Expand Down Expand Up @@ -67,8 +67,18 @@ - (instancetype)initForAutoLayout
Defaults to Required, will only be a different value while executing a constraints block passed into the
+[UIView autoSetPriority:forConstraints:] method (as that method will reset the value back to Required
before returning).
NOTE: As UIKit is not thread safe, access to this variable is not synchronized (and should only be done
on the main thread).
*/
static UILayoutPriority _globalConstraintPriority = UILayoutPriorityRequired;
static UILayoutPriority _al_globalConstraintPriority = UILayoutPriorityRequired;

/**
A global variable that is set to YES while the constraints block passed in to the
+[UIView autoSetPriority:forConstraints:] method is executing.
NOTE: As UIKit is not thread safe, access to this variable is not synchronized (and should only be done
on the main thread).
*/
static BOOL _al_isExecutingConstraintsBlock = NO;

/**
Sets the constraint priority to the given value for all constraints created using the UIView+AutoLayout
Expand All @@ -83,11 +93,13 @@ - (instancetype)initForAutoLayout
+ (void)autoSetPriority:(UILayoutPriority)priority forConstraints:(ALConstraintsBlock)block
{
NSAssert(block, @"The constraints block cannot be nil.");
_globalConstraintPriority = priority;
if (block) {
_al_globalConstraintPriority = priority;
_al_isExecutingConstraintsBlock = YES;
block();
_al_isExecutingConstraintsBlock = NO;
_al_globalConstraintPriority = UILayoutPriorityRequired;
}
_globalConstraintPriority = UILayoutPriorityRequired;
}


Expand Down Expand Up @@ -500,6 +512,39 @@ - (NSLayoutConstraint *)autoSetDimension:(ALDimension)dimension toSize:(CGFloat)
}


#pragma mark Set Content Compression Resistance & Hugging

/**
Sets the priority of content compression resistance for an axis.
NOTE: This method must only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:]
@param axis The axis to set the content compression resistance priority for.
*/
- (void)autoSetContentCompressionResistancePriorityForAxis:(ALAxis)axis
{
NSAssert(_al_isExecutingConstraintsBlock, @"%@ should only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
if (_al_isExecutingConstraintsBlock) {
UILayoutConstraintAxis constraintAxis = [UIView al_constraintAxisForAxis:axis];
[self setContentCompressionResistancePriority:_al_globalConstraintPriority forAxis:constraintAxis];
}
}

/**
Sets the priority of content hugging for an axis.
NOTE: This method must only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:]
@param axis The axis to set the content hugging priority for.
*/
- (void)autoSetContentHuggingPriorityForAxis:(ALAxis)axis
{
NSAssert(_al_isExecutingConstraintsBlock, @"%@ should only be called from within the block passed into the method +[UIView autoSetPriority:forConstraints:]", NSStringFromSelector(_cmd));
if (_al_isExecutingConstraintsBlock) {
UILayoutConstraintAxis constraintAxis = [UIView al_constraintAxisForAxis:axis];
[self setContentHuggingPriority:_al_globalConstraintPriority forAxis:constraintAxis];
}
}


#pragma mark Constrain Any Attributes

/**
Expand Down Expand Up @@ -720,7 +765,7 @@ - (NSLayoutConstraint *)autoPinEdge:(ALEdge)edge toPositionInSuperview:(CGFloat)
*/
- (void)al_addConstraintUsingGlobalPriority:(NSLayoutConstraint *)constraint
{
constraint.priority = _globalConstraintPriority;
constraint.priority = _al_globalConstraintPriority;
[self addConstraint:constraint];
}

Expand Down Expand Up @@ -805,6 +850,11 @@ + (NSLayoutAttribute)al_attributeForDimension:(ALDimension)dimension
return attribute;
}

/**
Returns the corresponding NSLayoutAttribute for the given ALAttribute.
@return The layout attribute for the given ALAttribute.
*/
+ (NSLayoutAttribute)al_attributeForALAttribute:(NSInteger)ALAttribute
{
NSLayoutAttribute attribute = NSLayoutAttributeNotAnAttribute;
Expand Down Expand Up @@ -849,6 +899,29 @@ + (NSLayoutAttribute)al_attributeForALAttribute:(NSInteger)ALAttribute
return attribute;
}

/**
Returns the corresponding UILayoutConstraintAxis for the given ALAxis.
@return The constraint axis for the given axis.
*/
+ (UILayoutConstraintAxis)al_constraintAxisForAxis:(ALAxis)axis
{
UILayoutConstraintAxis constraintAxis;
switch (axis) {
case ALAxisVertical:
constraintAxis = UILayoutConstraintAxisVertical;
break;
case ALAxisHorizontal:
case ALAxisBaseline:
constraintAxis = UILayoutConstraintAxisHorizontal;
break;
default:
NSAssert(nil, @"Not a valid ALAxis.");
break;
}
return constraintAxis;
}

/**
Returns the common superview for this view and the given peer view.
Raises an exception if this view and the peer view do not share a common superview.
Expand Down

0 comments on commit fa7ad11

Please sign in to comment.