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

Adds OAStackViewProxy to support native UIStackView in iOS 9 #32

Closed
wants to merge 1 commit into from
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
10 changes: 10 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.

35 changes: 35 additions & 0 deletions Pod/Classes/OAStackViewProxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// OAStackViewProxy.h
// Pods
//
// Created by Natan Rolnik on 8/5/15.
//
//

#import <UIKit/UIKit.h>

@interface OAStackViewProxy : UIView

@property(nonatomic,readonly,copy) NSArray<__kindof UIView *> *arrangedSubviews;

- (instancetype)initWithArrangedSubviews:(NSArray *)arrangedSubviews;

- (void)addArrangedSubview:(UIView *)view;

- (void)removeArrangedSubview:(UIView *)view;

- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex;

@property(nonatomic) UILayoutConstraintAxis axis;

@property(nonatomic) UIStackViewDistribution distribution;

@property(nonatomic) UIStackViewAlignment alignment;

@property(nonatomic) CGFloat spacing;

@property(nonatomic,getter=isBaselineRelativeArrangement) BOOL baselineRelativeArrangement;

@property(nonatomic,getter=isLayoutMarginsRelativeArrangement) BOOL layoutMarginsRelativeArrangement;

@end
167 changes: 167 additions & 0 deletions Pod/Classes/OAStackViewProxy.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//
// OAStackViewProxy.m
// Pods
//
// Created by Natan Rolnik on 8/5/15.
//
//

#import "OAStackView.h"
#import "OAStackViewProxy.h"

@interface OAStackViewProxy()

@property (nonatomic, strong) UIView *stackView;
@property (nonatomic, strong) NSArray<UIView *> *initialArrangedSubviews;

@property (nonatomic, strong) UIStackView *nativeStackView;
@property (nonatomic, strong) OAStackView *backwardsCompatibleStackView;

@end

@implementation OAStackViewProxy

+ (BOOL)nativeStackViewAvailable
{
return NSClassFromString(@"UIStackView") != nil;
}

#pragma mark - Initializers

- (instancetype)init
{
self = [self initWithArrangedSubviews:nil];

[self commonInit];

return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

[self commonInit];

return self;
}

- (instancetype)initWithCoder:(nonnull NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

[self commonInit];

return self;
}

- (instancetype)initWithArrangedSubviews:(NSArray *)arrangedSubviews
{
self = [super initWithFrame:CGRectZero];

self.initialArrangedSubviews = arrangedSubviews;

[self commonInit];

return self;
}

- (void)commonInit
{
[self createStackView];
[self addSubview:self.stackView];
}

- (void)createStackView
{
if (!self.stackView) {
if ([OAStackViewProxy nativeStackViewAvailable]) {
self.nativeStackView = [[UIStackView alloc] initWithArrangedSubviews:self.initialArrangedSubviews];
self.stackView = self.nativeStackView;
}
else {
self.backwardsCompatibleStackView = [[OAStackView alloc] initWithArrangedSubviews:self.initialArrangedSubviews];
self.stackView = self.backwardsCompatibleStackView;
}
}
}

#pragma mark - Runtime

- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature *signature = [super methodSignatureForSelector:selector];

if (!signature) {
signature = [self.stackView methodSignatureForSelector:selector];
}

return signature;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.stackView respondsToSelector:[anInvocation selector]]) {
[anInvocation invokeWithTarget:self.stackView];
}
else {
[super forwardInvocation:anInvocation];
}
}

#pragma mark - Setters

- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];

self.stackView.frame = self.bounds;
}

- (void)setAxis:(UILayoutConstraintAxis)axis
{
_axis = axis;

self.nativeStackView.axis = axis;
self.backwardsCompatibleStackView.axis = axis;
}

- (void)setDistribution:(UIStackViewDistribution)distribution
{
_distribution = distribution;

self.nativeStackView.distribution = distribution;
self.backwardsCompatibleStackView.distribution = distribution;
}

- (void)setAlignment:(UIStackViewAlignment)alignment
{
_alignment = alignment;

self.nativeStackView.alignment = alignment;
self.backwardsCompatibleStackView.alignment = alignment;
}

- (void)setSpacing:(CGFloat)spacing
{
_spacing = spacing;

self.nativeStackView.spacing = spacing;
self.backwardsCompatibleStackView.spacing = spacing;
}

- (void)setBaselineRelativeArrangement:(BOOL)baselineRelativeArrangement
{
_baselineRelativeArrangement = baselineRelativeArrangement;

self.nativeStackView.baselineRelativeArrangement = baselineRelativeArrangement;
}

- (void)setLayoutMarginsRelativeArrangement:(BOOL)layoutMarginsRelativeArrangement
{
_layoutMarginsRelativeArrangement = layoutMarginsRelativeArrangement;

self.nativeStackView.layoutMarginsRelativeArrangement = layoutMarginsRelativeArrangement;
}

@end