Last active
August 29, 2015 14:23
-
-
Save jonathan-beebe/768a5692095fcf07cef7 to your computer and use it in GitHub Desktop.
A set of NSArray category methods (for arrays of UIViews) to automatically layout auto-hugging rows or columns. Requires PureLayout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example — say you had some content for a scrollView. | |
// Create a contentView and add each row as a subview. | |
// Then simply call the category method on the subviews array to layout | |
// the subviews as rows. | |
// | |
// This requires that the contentView be setup via autolayout to | |
// properly pin to the scrollView. | |
[contentView addSubviews:@[ | |
viewA, | |
viewB, | |
viewC | |
]]; | |
// Pin each subview as a full-width, flexible-height row. | |
[contentView.subviews pinAutoHuggingRows]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSArray (EDLayout) | |
- (NSArray*)pinAutoHuggingRows; | |
- (NSArray*)pinAutoHuggingRowsWithSeparation:(CGFloat)separation; | |
- (NSArray*)pinAutoHuggingColumns; | |
- (NSArray*)pinAutoHuggingColumnsWithSeparation:(CGFloat)separation; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef NS_ENUM (NSInteger, EDLayoutViewPosition) { | |
EDLayoutViewPositionLonely, | |
EDLayoutViewPositionFirst, | |
EDLayoutViewPositionMiddle, | |
EDLayoutViewPositionLast | |
}; | |
@implementation NSArray (EDLayout) | |
- (NSArray*)pinAutoHuggingRows | |
{ | |
return [self pinAutoHuggingRowsWithSeparation:0.0]; | |
} | |
- (NSArray*)pinAutoHuggingRowsWithSeparation:(CGFloat)separation | |
{ | |
[self iterateViewsWithBlock:^(EDLayoutViewPosition position, UIView* previousView, UIView* view, UIView* nextView) { | |
[view pinFullWidth]; | |
switch (position) { | |
case EDLayoutViewPositionLonely: | |
[view pinFullHeight]; | |
break; | |
case EDLayoutViewPositionFirst: | |
[view pinTop]; | |
break; | |
case EDLayoutViewPositionMiddle: | |
[view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView withOffset:separation]; | |
break; | |
case EDLayoutViewPositionLast: | |
[view pinBottom]; | |
[view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView withOffset:separation]; | |
break; | |
} | |
[view autoHugContent]; | |
}]; | |
return self; | |
} | |
- (NSArray*)pinAutoHuggingColumns | |
{ | |
return [self pinAutoHuggingColumnsWithSeparation:0.0]; | |
} | |
- (NSArray*)pinAutoHuggingColumnsWithSeparation:(CGFloat)separation | |
{ | |
[self iterateViewsWithBlock:^(EDLayoutViewPosition position, UIView* previousView, UIView* view, UIView* nextView) { | |
[view pinFullHeight]; | |
switch (position) { | |
case EDLayoutViewPositionLonely: | |
[view pinFullWidth]; | |
break; | |
case EDLayoutViewPositionFirst: | |
[view pinLeft]; | |
break; | |
case EDLayoutViewPositionMiddle: | |
[view autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:previousView withOffset:separation]; | |
break; | |
case EDLayoutViewPositionLast: | |
[view pinRight]; | |
[view autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:previousView withOffset:separation]; | |
break; | |
} | |
[view autoHugContent]; | |
}]; | |
return self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment