Skip to content

Commit

Permalink
Add new API variants to distribute views with optional insets to thei…
Browse files Browse the repository at this point in the history
…r superview

This commit also merges pull request #12 by @rahul-malik.
  • Loading branch information
smileyborg committed Apr 21, 2014
1 parent 9cc25a4 commit 37b43ee
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Example/Example/ALViewController.m
Expand Up @@ -88,7 +88,7 @@ - (void)setupDemo1

[self.orangeView autoAlignAxisToSuperviewAxis:ALAxisVertical];

[subviews autoDistributeViewsAlongAxis:ALAxisVertical withFixedSize:30.0f alignment:NSLayoutFormatAlignAllCenterX];
[subviews autoDistributeViewsAlongAxis:ALAxisVertical withFixedSize:30.0f insetSpacing:YES alignment:NSLayoutFormatAlignAllCenterX];
}

/**
Expand All @@ -106,7 +106,7 @@ - (void)setupDemo2

[self.orangeView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];

[subviews autoDistributeViewsAlongAxis:ALAxisHorizontal withFixedSpacing:10.0f alignment:NSLayoutFormatAlignAllCenterY];
[subviews autoDistributeViewsAlongAxis:ALAxisHorizontal withFixedSpacing:10.0f insetSpacing:NO alignment:NSLayoutFormatAlignAllCenterY];
}

/**
Expand Down
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -37,8 +37,8 @@ This is just a handy overview of the core API methods. Check out the [header fil
- autoAlignViewsToAxis:
- autoMatchViewsDimension:
- autoSetViewsDimension:toSize:
- autoDistributeViewsAlongAxis:withFixedSpacing:alignment:
- autoDistributeViewsAlongAxis:withFixedSize:alignment:
- autoDistributeViewsAlongAxis:withFixedSpacing:(insetSpacing:)alignment:
- autoDistributeViewsAlongAxis:withFixedSize:(insetSpacing:)alignment:

**`NSLayoutConstraint`**

Expand Down Expand Up @@ -115,6 +115,8 @@ Problems, Suggestions, Pull Requests?

Bring 'em on! :)

It's always a good idea to reach out before taking on any significant amount of changes or additions to the project. This allows everyone to get onboard with upcoming changes, ensures that changes align with the project's design philosophy, and avoids duplicated work.

I'm especially interested in hearing about any common use cases that this API does not currently address. Feel free to add feature requests (and view current work in progress) on the [Feature Requests](https://github.com/smileyborg/UIView-AutoLayout/wiki/Feature-Requests) page of the wiki for this project.

Meta
Expand Down
6 changes: 6 additions & 0 deletions Source/UIView+AutoLayout.h
Expand Up @@ -266,9 +266,15 @@ typedef void(^ALConstraintsBlock)(void); // a block of method calls to the UI
/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing alignment:(NSLayoutFormatOptions)alignment;

/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them, with optional insets from the first and last views to their superview. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing insetSpacing:(BOOL)shouldSpaceInsets alignment:(NSLayoutFormatOptions)alignment;

/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)size alignment:(NSLayoutFormatOptions)alignment;

/** Distributes the views in this array equally along the selected axis in their superview. Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them, with optional insets from the first and last views to their superview. */
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)size insetSpacing:(BOOL)shouldSpaceInsets alignment:(NSLayoutFormatOptions)alignment;

@end


Expand Down
57 changes: 51 additions & 6 deletions Source/UIView+AutoLayout.m
Expand Up @@ -1129,16 +1129,33 @@ - (NSArray *)autoSetViewsDimension:(ALDimension)dimension toSize:(CGFloat)size

#pragma mark Distribute Multiple Views

/**
Distributes the views in this array equally along the selected axis in their superview.
Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them,
including from the first and last views to their superview.
@param axis The axis along which to distribute the subviews.
@param spacing The fixed amount of spacing between each subview, before the first subview and after the last subview.
@param alignment The way in which the subviews will be aligned.
@return An array of constraints added.
*/
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing alignment:(NSLayoutFormatOptions)alignment
{
return [self autoDistributeViewsAlongAxis:axis withFixedSpacing:spacing insetSpacing:YES alignment:alignment];
}

/**
Distributes the views in this array equally along the selected axis in their superview.
Views will be the same size (variable) in the dimension along the axis and will have spacing (fixed) between them.
The first and last views can optionally be inset from their superview by the same amount of spacing as between views.
@param axis The axis along which to distribute the subviews.
@param spacing The fixed amount of spacing between each subview.
@param shouldSpaceInsets Whether the first and last views should be equally inset from their superview.
@param alignment The way in which the subviews will be aligned.
@return An array of constraints added.
*/
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing alignment:(NSLayoutFormatOptions)alignment
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat)spacing insetSpacing:(BOOL)shouldSpaceInsets alignment:(NSLayoutFormatOptions)alignment
{
NSAssert([self al_containsMinimumNumberOfViews:2], @"This array must contain at least 2 views to distribute.");
ALDimension matchedDimension;
Expand All @@ -1159,39 +1176,61 @@ - (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSpacing:(CGFloat
NSAssert(nil, @"Not a valid axis.");
return nil;
}
CGFloat leadingSpacing = shouldSpaceInsets ? spacing : 0.0;
CGFloat trailingSpacing = shouldSpaceInsets ? spacing : 0.0;

NSMutableArray *constraints = [NSMutableArray new];
UIView *previousView = nil;
for (id object in self) {
if ([object isKindOfClass:[UIView class]]) {
UIView *view = (UIView *)object;
if (previousView) {
// Second, Third, ... View
[constraints addObject:[view autoPinEdge:firstEdge toEdge:lastEdge ofView:previousView withOffset:spacing]];
[constraints addObject:[view autoMatchDimension:matchedDimension toDimension:matchedDimension ofView:previousView]];
[constraints addObject:[view al_alignToView:previousView withOption:alignment forAxis:axis]];
}
else {
[constraints addObject:[view autoPinEdgeToSuperviewEdge:firstEdge withInset:spacing]];
// First view
[constraints addObject:[view autoPinEdgeToSuperviewEdge:firstEdge withInset:leadingSpacing]];
}
previousView = view;
}
}
if (previousView) {
[constraints addObject:[previousView autoPinEdgeToSuperviewEdge:lastEdge withInset:spacing]];
// Last View
[constraints addObject:[previousView autoPinEdgeToSuperviewEdge:lastEdge withInset:trailingSpacing]];
}
return constraints;
}

/**
Distributes the views in this array equally along the selected axis in their superview.
Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them.
Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them,
including from the first and last views to their superview.
@param axis The axis along which to distribute the subviews.
@param size The fixed size of each subview in the dimension along the given axis.
@param alignment The way in which the subviews will be aligned.
@return An array of constraints added.
*/
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)size alignment:(NSLayoutFormatOptions)alignment
{
return [self autoDistributeViewsAlongAxis:axis withFixedSize:size insetSpacing:YES alignment:alignment];
}

/**
Distributes the views in this array equally along the selected axis in their superview.
Views will be the same size (fixed) in the dimension along the axis and will have spacing (variable) between them.
The first and last views can optionally be inset from their superview by the same amount of spacing as between views.
@param axis The axis along which to distribute the subviews.
@param size The fixed size of each subview in the dimension along the given axis.
@param shouldSpaceInsets Whether the first and last views should be equally inset from their superview.
@param alignment The way in which the subviews will be aligned.
@return An array of constraints added.
*/
- (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)size insetSpacing:(BOOL)shouldSpaceInsets alignment:(NSLayoutFormatOptions)alignment
{
NSAssert([self al_containsMinimumNumberOfViews:2], @"This array must contain at least 2 views to distribute.");
ALDimension fixedDimension;
Expand Down Expand Up @@ -1221,8 +1260,14 @@ - (NSArray *)autoDistributeViewsAlongAxis:(ALAxis)axis withFixedSize:(CGFloat)si
for (NSInteger i = 0; i < numberOfViews; i++) {
UIView *view = shouldFlipOrder ? views[numberOfViews - i - 1] : views[i];
[constraints addObject:[view autoSetDimension:fixedDimension toSize:size]];
CGFloat multiplier = (i * 2.0f + 2.0f) / (numberOfViews + 1.0f);
CGFloat constant = (multiplier - 1.0f) * size / 2.0f;
CGFloat multiplier, constant;
if (shouldSpaceInsets) {
multiplier = (i * 2.0f + 2.0f) / (numberOfViews + 1.0f);
constant = (multiplier - 1.0f) * size / 2.0f;
} else {
multiplier = (i * 2.0f) / (numberOfViews - 1.0f);
constant = (-multiplier + 1.0f) * size / 2.0f;
}
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view attribute:attribute relatedBy:NSLayoutRelationEqual toItem:commonSuperview attribute:attribute multiplier:multiplier constant:constant];
[commonSuperview al_addConstraintUsingGlobalPriority:constraint];
[constraints addObject:constraint];
Expand Down

0 comments on commit 37b43ee

Please sign in to comment.