Skip to content

Commit

Permalink
Use native layout margins when available
Browse files Browse the repository at this point in the history
This makes `preservesSuperviewLayoutMargins` work properly on supported
systems (iOS 8+).
  • Loading branch information
Nikolay Kasyanov committed Mar 28, 2017
1 parent 1bb8b71 commit 7d6268a
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Pod/Classes/OAStackView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ @interface OAStackView ()

// Not implemented but needed for backward compatibility with UIStackView
@property(nonatomic,getter=isBaselineRelativeArrangement) BOOL baselineRelativeArrangement;

@property(nonatomic, getter=areNativeLayoutMarginsSupported, readonly) BOOL nativeLayoutMarginsSupported;

@end

@implementation OAStackView
Expand Down Expand Up @@ -69,14 +72,16 @@ - (instancetype)initWithFrame:(CGRect)frame {
}

- (void)commonInitWithInitalSubviews:(NSArray *)initialSubviews {
_nativeLayoutMarginsSupported = [UIView instancesRespondToSelector:@selector(layoutMargins)];

_mutableArrangedSubviews = [initialSubviews mutableCopy];
[self addViewsAsSubviews:initialSubviews];

_axis = UILayoutConstraintAxisHorizontal;
_alignment = OAStackViewAlignmentFill;
_distribution = OAStackViewDistributionFill;

_layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);
_compatibilityLayoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);
_layoutMarginsRelativeArrangement = NO;

self.alignmentStrategy = [OAStackViewAlignmentStrategy strategyWithStackView:self];
Expand Down Expand Up @@ -211,9 +216,23 @@ - (void)setDistributionValue:(NSInteger)distributionValue {
self.distribution = distributionValue;
}

@synthesize layoutMargins = _compatibilityLayoutMargins;

- (UIEdgeInsets)layoutMargins {
if (self.nativeLayoutMarginsSupported) {
return super.layoutMargins;
}

return _compatibilityLayoutMargins;
}

- (void)setLayoutMargins:(UIEdgeInsets)layoutMargins {
_layoutMargins = layoutMargins;
[self layoutArrangedViews];
if (self.nativeLayoutMarginsSupported) {
[super setLayoutMargins:layoutMargins];
} else {
_compatibilityLayoutMargins = layoutMargins;
[self layoutArrangedViews];
}
}

- (void)setLayoutMarginsRelativeArrangement:(BOOL)layoutMarginsRelativeArrangement {
Expand All @@ -227,6 +246,11 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[self layoutArrangedViews];
}

- (void)layoutMarginsDidChange {
[super layoutMarginsDidChange];
[self layoutArrangedViews];
}

#pragma mark - Adding and removing

- (void)addArrangedSubview:(UIView *)view {
Expand Down

0 comments on commit 7d6268a

Please sign in to comment.