Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-beebe/768a5692095fcf07cef7 to your computer and use it in GitHub Desktop.
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.
// 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];
@interface NSArray (EDLayout)
- (NSArray*)pinAutoHuggingRows;
- (NSArray*)pinAutoHuggingRowsWithSeparation:(CGFloat)separation;
- (NSArray*)pinAutoHuggingColumns;
- (NSArray*)pinAutoHuggingColumnsWithSeparation:(CGFloat)separation;
@end
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