Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fixes issues with the arrangedSubviews property #17

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions Pod/Classes/OAStackView+Constraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ - (NSArray*)constraintsBetweenView:(UIView*)firstView andView:(UIView*)otherView
viewMatches = viewMatches || (firstView == constraint.secondItem && otherView == constraint.firstItem);
}

BOOL isCorrectAxis = [self isConstraintAttribute:constraint.firstAttribute affectingAxis:axis] ||
[self isConstraintAttribute:constraint.secondAttribute affectingAxis:axis];
BOOL isCorrectAxis = [self isConstraint:constraint affectingAxis:axis];

if (viewMatches && isCorrectAxis) {
[arr addObject:constraint];
Expand Down Expand Up @@ -141,15 +140,29 @@ - (BOOL)isConstraintAttribute:(NSLayoutAttribute)attribute affectingAxis:(UILayo
case UILayoutConstraintAxisHorizontal:
return attribute == NSLayoutAttributeLeft || attribute == NSLayoutAttributeRight ||
attribute == NSLayoutAttributeLeading || attribute == NSLayoutAttributeTrailing ||
attribute == NSLayoutAttributeCenterX || attribute == NSLayoutAttributeWidth;
attribute == NSLayoutAttributeCenterX || attribute == NSLayoutAttributeWidth
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
|| attribute == NSLayoutAttributeLeftMargin || attribute == NSLayoutAttributeRightMargin ||
attribute == NSLayoutAttributeLeadingMargin || attribute == NSLayoutAttributeTrailingMargin ||
attribute == NSLayoutAttributeCenterXWithinMargins
#endif
;
break;

case UILayoutConstraintAxisVertical:
return attribute == NSLayoutAttributeTop || attribute == NSLayoutAttributeBottom ||
attribute == NSLayoutAttributeCenterY || attribute == NSLayoutAttributeHeight;
attribute == NSLayoutAttributeCenterY || attribute == NSLayoutAttributeHeight ||
attribute == NSLayoutAttributeBaseline
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
|| attribute == NSLayoutAttributeLastBaseline || attribute == NSLayoutAttributeFirstBaseline ||
attribute == NSLayoutAttributeTopMargin || attribute == NSLayoutAttributeBottomMargin ||
attribute == NSLayoutAttributeCenterYWithinMargins
#endif
;
break;

default:
return NO;
break;
}
}
Expand Down
12 changes: 0 additions & 12 deletions Pod/Classes/OAStackView+Hiding.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,4 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

}

#pragma mark subviews

- (void)didAddSubview:(UIView *)subview {
[super didAddSubview:subview];
[self addObserverForView:subview];
}

- (void)willRemoveSubview:(UIView *)subview {
[super willRemoveSubview:subview];
[self removeObserverForView:subview];
}

@end
12 changes: 5 additions & 7 deletions Pod/Classes/OAStackView+Traversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

@interface OAStackView (Traversal)

- (UIView*)visibleViewBeforeIndex:(NSInteger)index;
- (UIView*)visibleViewBeforeView:(UIView*)view;
- (UIView*)arrangedSubviewBeforeIndex:(NSInteger)index;
- (UIView*)arrangedSubviewBeforeView:(UIView*)view;

- (UIView*)visibleViewAfterIndex:(NSInteger)index;
- (UIView*)visibleViewAfterView:(UIView*)view;
- (UIView*)arrangedSubviewAfterIndex:(NSInteger)index;
- (UIView*)arrangedSubviewAfterView:(UIView*)view;

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

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

- (NSLayoutConstraint*)firstViewConstraint;
- (NSLayoutConstraint*)lastViewConstraint;
Expand Down
58 changes: 18 additions & 40 deletions Pod/Classes/OAStackView+Traversal.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,50 @@
//

#import "OAStackView+Traversal.h"
#import "OAStackView_ArrangedSubviews.h"

@implementation OAStackView (Traversal)

- (UIView*)visibleViewBeforeView:(UIView*)view {
NSInteger index = [self.subviews indexOfObject:view];
- (UIView*)arrangedSubviewBeforeView:(UIView*)view {
NSInteger index = [self indexInArrangedSubviewsOfObject:view];
if (index == NSNotFound) { return nil; }

return [self visibleViewBeforeIndex:index];
return [self arrangedSubviewBeforeIndex:index];
}

- (UIView*)visibleViewAfterView:(UIView*)view {
NSInteger index = [self.subviews indexOfObject:view];
- (UIView*)arrangedSubviewAfterView:(UIView*)view {
NSInteger index = [self indexInArrangedSubviewsOfObject:view];
if (index == NSNotFound) { return nil; }

return [self visibleViewAfterIndex:index];
return [self arrangedSubviewAfterIndex:index];
}

- (UIView*)visibleViewAfterIndex:(NSInteger)index {
for (NSInteger i = index + 1; i < self.subviews.count; i++) {
UIView *theView = self.subviews[i];
if (!theView.hidden) {
return theView;
}
- (UIView*)arrangedSubviewAfterIndex:(NSInteger)index {
if ((index + 1) < [self countOfArrangedSubviews]) {
return [self objectInArrangedSubviewsAtIndex:index+1];
}

return nil;
}

- (UIView*)visibleViewBeforeIndex:(NSInteger)index {
for (NSInteger i = index - 1; i >= 0; i--) {
UIView *theView = self.subviews[i];
if (!theView.hidden) {
return theView;
}
- (UIView*)arrangedSubviewBeforeIndex:(NSInteger)index {
if (index > 0) {
return [self objectInArrangedSubviewsAtIndex:index-1];
}

return nil;
}

- (UIView*)lastVisibleItem {
return [self visibleViewBeforeIndex:self.subviews.count];
}

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

id previousView;
for (UIView *view in self.subviews) {
if (view.isHidden) { continue; }
- (void)iterateArrangedSubviews:(void (^) (UIView *view, UIView *previousView))block {
UIView *previousView = nil;
for (NSUInteger i = 0; i < [self countOfArrangedSubviews]; i++) {
UIView *view = [self objectInArrangedSubviewsAtIndex:i];

block(view, previousView);
previousView = view;
}
}

- (NSArray*)currentVisibleViews {
NSMutableArray *arr = [@[] mutableCopy];
[self iterateVisibleViews:^(UIView *view, UIView *previousView) {
[arr addObject:view];
}];
return arr;
}

- (BOOL)isLastVisibleItem:(UIView*)view {
return view == [self lastVisibleItem];
}

- (NSLayoutConstraint*)lastViewConstraint {
for (NSLayoutConstraint *constraint in self.constraints) {

Expand Down Expand Up @@ -113,7 +91,7 @@ - (NSLayoutConstraint*)firstViewConstraint {
}

- (BOOL)isViewLastItem:(UIView*)view excludingItem:(UIView*)excludingItem {
NSArray *visible = [self currentVisibleViews];
NSArray *visible = [self arrangedSubviews];
NSInteger index = [visible indexOfObject:view];
NSInteger exclutedIndex = [visible indexOfObject:excludingItem];

Expand Down
5 changes: 4 additions & 1 deletion Pod/Classes/OAStackView.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ typedef NS_ENUM(NSInteger, OAStackViewAlignment) {
#define NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_END
#define nullable
#define nonnullable
#define nonnull
#define __nullable
#define __nonnull
#endif

NS_ASSUME_NONNULL_BEGIN
@interface OAStackView : UIView

// The views that are currently being arranged.
// Views that are hidden will be removed from this list
@property(nonatomic,readonly,copy) NSArray *arrangedSubviews;

//Default is Vertical
Expand Down
Loading