Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pod/Classes/OAStackView+Traversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

- (void)iterateVisibleViews:(void (^) (UIView *view, UIView *previousView))block;

- (NSArray*)currentVisibleViews;

- (UIView*)lastVisibleItem;

- (NSLayoutConstraint*)firstViewConstraint;
Expand Down
5 changes: 3 additions & 2 deletions Pod/Classes/OAStackView.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef NS_ENUM(NSInteger, OAStackViewAlignment) {
*/
OAStackViewAlignmentLeading,
OAStackViewAlignmentTop = OAStackViewAlignmentLeading,
OAStackViewAlignmentFirstBaseline, // Valid for horizontal axis only
OAStackViewAlignmentFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0), // Valid for horizontal axis only

/* Center the items in a vertical stack horizontally
or the items in a horizontal stack vertically
Expand All @@ -68,7 +68,8 @@ typedef NS_ENUM(NSInteger, OAStackViewAlignment) {
*/
OAStackViewAlignmentTrailing,
OAStackViewAlignmentBottom = OAStackViewAlignmentTrailing,
OAStackViewAlignmentLastBaseline, // Valid for horizontal axis only
OAStackViewAlignmentBaseline,
OAStackViewAlignmentLastBaseline = OAStackViewAlignmentBaseline, // Valid for horizontal axis only
};

// Keep older versions of the compiler happy
Expand Down
4 changes: 4 additions & 0 deletions Pod/Classes/OAStackView.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ - (void)setAlignment:(OAStackViewAlignment)alignment {

[self iterateVisibleViews:^(UIView *view, UIView *previousView) {
[self.alignmentStrategy addConstraintsOnOtherAxis:view];
[self.alignmentStrategy alignView:view withPreviousView:previousView];
}];
}

Expand All @@ -134,6 +135,7 @@ - (void)setDistribution:(OAStackViewDistribution)distribution {
[self iterateVisibleViews:^(UIView *view, UIView *previousView) {
[self.alignmentStrategy addConstraintsOnOtherAxis:view];
[self.distributionStrategy alignView:view afterView:previousView];
[self.alignmentStrategy alignView:view withPreviousView:previousView];
}];
}

Expand Down Expand Up @@ -239,8 +241,10 @@ - (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex newI
}

[self.distributionStrategy alignView:view afterView:previousView];
[self.alignmentStrategy alignView:view withPreviousView:previousView];
[self.alignmentStrategy addConstraintsOnOtherAxis:view];
[self.distributionStrategy alignView:nextView afterView:view];
[self.alignmentStrategy alignView:nextView withPreviousView:view];
}

- (void)removeViewFromArrangedViews:(UIView*)view permanently:(BOOL)permanently {
Expand Down
4 changes: 3 additions & 1 deletion Pod/Classes/OAStackViewAlignmentStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
+ (OAStackViewAlignmentStrategy*)strategyWithStackView:(OAStackView *)stackView;

- (void)addConstraintsOnOtherAxis:(UIView*)view;
- (void)removeAddedConstraints;;
- (void)alignView:(UIView*)view withPreviousView:(UIView*)previousView;

- (void)removeAddedConstraints;

@end
72 changes: 70 additions & 2 deletions Pod/Classes/OAStackViewAlignmentStrategy.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ @interface OAStackViewAlignmentStrategyLeading : OAStackViewAlignmentStrategy
@interface OAStackViewAlignmentStrategyCenter: OAStackViewAlignmentStrategy
@end

@interface OAStackViewAlignmentStrategyBaseline: OAStackViewAlignmentStrategy
- (NSLayoutAttribute)baselineAttribute;
@end

@interface OAStackViewAlignmentStrategyLastBaseline: OAStackViewAlignmentStrategyBaseline
@end

@interface OAStackViewAlignmentStrategyFirstBaseline: OAStackViewAlignmentStrategyBaseline
@end

@interface OAStackViewAlignmentStrategy ()
@property(nonatomic, weak) OAStackView *stackView;
@property(nonatomic) NSMutableArray *constraints;
Expand Down Expand Up @@ -47,7 +57,15 @@ + (OAStackViewAlignmentStrategy*)strategyWithStackView:(OAStackView *)stackView
case OAStackViewAlignmentCenter:
cls = [OAStackViewAlignmentStrategyCenter class];
break;


case OAStackViewAlignmentBaseline:
cls = [OAStackViewAlignmentStrategyLastBaseline class];
break;

case OAStackViewAlignmentFirstBaseline:
cls = [OAStackViewAlignmentStrategyFirstBaseline class];
break;

default:
break;
}
Expand Down Expand Up @@ -75,7 +93,14 @@ - (void)addConstraintsOnOtherAxis:(UIView*)view {
id arr = [self constraintsalignViewOnOtherAxis:view];
[self.constraints addObjectsFromArray:arr];

[self.stackView addConstraints:arr];
if (arr) { [self.stackView addConstraints:arr]; }
}

- (void)alignView:(UIView*)view withPreviousView:(UIView*)previousView {
id arr = [self constraintsAlignView:view afterPreviousView:previousView];
[self.constraints addObjectsFromArray:arr];

if (arr) { [self.stackView addConstraints:arr]; }
}

- (NSMutableArray *)constraints {
Expand All @@ -93,6 +118,8 @@ - (void)removeAddedConstraints {

- (NSArray*)constraintsalignViewOnOtherAxis:(UIView*)view { /* subclassing */ return nil; }

- (NSArray*)constraintsAlignView:(UIView *)view afterPreviousView:(UIView*)afterView { /* subclassing */ return nil; }

@end

@implementation OAStackViewAlignmentStrategyFill
Expand Down Expand Up @@ -150,3 +177,44 @@ - (NSArray*)constraintsalignViewOnOtherAxis:(UIView*)view {
}

@end

@implementation OAStackViewAlignmentStrategyBaseline

- (NSArray*)constraintsalignViewOnOtherAxis:(UIView*)view {
id constraintString = [NSString stringWithFormat:@"%@:|-(>=0@750)-[view]-(>=0@750)-|", [self otherAxisString]];

return [NSLayoutConstraint constraintsWithVisualFormat:constraintString
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(view)];
}

- (NSArray*)constraintsAlignView:(UIView *)view afterPreviousView:(UIView*)afterView {
if (!view || !afterView) { return nil; }

return @[[NSLayoutConstraint constraintWithItem:view
attribute:[self baselineAttribute]
relatedBy:NSLayoutRelationEqual toItem:afterView
attribute:[self baselineAttribute] multiplier:1.0f
constant:0.0f]];
}

- (NSLayoutAttribute)baselineAttribute
{
return NSLayoutAttributeBaseline;
}

@end

@implementation OAStackViewAlignmentStrategyFirstBaseline

- (NSLayoutAttribute)baselineAttribute
{
return NSLayoutAttributeFirstBaseline;
}

@end

@implementation OAStackViewAlignmentStrategyLastBaseline

@end