-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathOAStackView.m
More file actions
407 lines (308 loc) · 11.7 KB
/
OAStackView.m
File metadata and controls
407 lines (308 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// OAStackView.m
// OAStackView
//
// Created by Omar Abdelhafith on 14/06/2015.
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
//
#import "OAStackView.h"
#import "OAStackView+Constraint.h"
#import "OAStackView+Hiding.h"
#import "OAStackView+Traversal.h"
#import "OAStackViewAlignmentStrategy.h"
#import "OAStackViewDistributionStrategy.h"
#import "OATransformLayer.h"
#import <objc/runtime.h>
@interface OAStackView ()
@property(nonatomic, strong) NSMutableArray *mutableArrangedSubviews;
@property(nonatomic) OAStackViewAlignmentStrategy *alignmentStrategy;
@property(nonatomic) OAStackViewDistributionStrategy *distributionStrategy;
// 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
+ (Class)layerClass {
return [OATransformLayer class];
}
#pragma mark - Initialization
- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self) {
[self commonInitWithInitalSubviews:@[]];
if ([NSStringFromClass([self class]) isEqualToString:@"UIStackView"]) {
self.axis = [decoder decodeIntegerForKey:@"UIStackViewAxis"];
self.distribution = [decoder decodeIntegerForKey:@"UIStackViewDistribution"];
self.alignment = [decoder decodeIntegerForKey:@"UIStackViewAlignment"];
self.spacing = [decoder decodeDoubleForKey:@"UIStackViewSpacing"];
self.baselineRelativeArrangement = [decoder decodeBoolForKey:@"UIStackViewBaselineRelative"];
self.layoutMarginsRelativeArrangement = [decoder decodeBoolForKey:@"UIStackViewLayoutMarginsRelative"];
}
[self layoutArrangedViews];
}
return self;
}
- (instancetype)initWithArrangedSubviews:(NSArray<__kindof UIView *> *)views {
self = [super initWithFrame:CGRectZero];
if (self) {
[self commonInitWithInitalSubviews:views];
[self layoutArrangedViews];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
return [self initWithArrangedSubviews:@[]];
}
- (void)commonInitWithInitalSubviews:(NSArray *)initialSubviews {
_nativeLayoutMarginsSupported = [UIView instancesRespondToSelector:@selector(layoutMargins)];
_mutableArrangedSubviews = [initialSubviews mutableCopy];
[self addViewsAsSubviews:initialSubviews];
_axis = UILayoutConstraintAxisHorizontal;
_alignment = OAStackViewAlignmentFill;
_distribution = OAStackViewDistributionFill;
_compatibilityLayoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);
_layoutMarginsRelativeArrangement = NO;
self.alignmentStrategy = [OAStackViewAlignmentStrategy strategyWithStackView:self];
self.distributionStrategy = [OAStackViewDistributionStrategy strategyWithStackView:self];
}
#pragma mark - Properties
- (NSArray *)arrangedSubviews {
return self.mutableArrangedSubviews.copy;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
// Does not have any effect because `CATransformLayer` is not rendered.
}
- (void)setOpaque:(BOOL)opaque {
// Does not have any effect because `CATransformLayer` is not rendered.
}
- (void)setClipsToBounds:(BOOL)clipsToBounds {
// Does not have any effect because `CATransformLayer` is not rendered.
}
- (void)setSpacing:(CGFloat)spacing {
if (_spacing == spacing) { return; }
_spacing = spacing;
for (NSLayoutConstraint *constraint in self.constraints) {
BOOL isWidthOrHeight =
(constraint.firstAttribute == NSLayoutAttributeWidth) ||
(constraint.firstAttribute == NSLayoutAttributeHeight);
if ([self.subviews containsObject:constraint.firstItem] &&
[self.subviews containsObject:constraint.secondItem] &&
!isWidthOrHeight) {
constraint.constant = spacing;
}
}
}
- (void)setAxis:(UILayoutConstraintAxis)axis {
if (_axis == axis) { return; }
_axis = axis;
[self layoutArrangedViews];
}
- (void)setAxisValue:(NSInteger)axisValue {
_axisValue = axisValue;
self.axis = self.axisValue;
}
- (void)setAlignment:(OAStackViewAlignment)alignment {
if (_alignment == alignment) { return; }
_alignment = alignment;
[self setAlignmentConstraints];
}
- (void)setAlignmentConstraints {
self.alignmentStrategy = [OAStackViewAlignmentStrategy strategyWithStackView:self];
[self.alignmentStrategy alignFirstView:self.subviews.firstObject];
[self iterateVisibleViews:^(UIView *view, UIView *previousView) {
[self.alignmentStrategy addConstraintsOnOtherAxis:view];
[self.alignmentStrategy alignView:view withPreviousView:previousView];
}];
[self.alignmentStrategy alignLastView:self.subviews.lastObject];
}
- (void)setAlignmentStrategy:(OAStackViewAlignmentStrategy *)alignmentStrategy {
if ([_alignmentStrategy isEqual:alignmentStrategy]) {
return;
}
[_alignmentStrategy removeAddedConstraints];
_alignmentStrategy = alignmentStrategy;
}
- (void)setDistributionStrategy:(OAStackViewDistributionStrategy *)distributionStrategy {
if ([_distributionStrategy isEqual:distributionStrategy]) {
return;
}
[_distributionStrategy removeAddedConstraints];
_distributionStrategy = distributionStrategy;
}
- (void)removeConstraint:(NSLayoutConstraint *)constraint {
[super removeConstraint:constraint];
}
- (void)removeConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints {
[super removeConstraints:constraints];
}
- (void)updateConstraints {
[super updateConstraints];
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (void)setAlignmentValue:(NSInteger)alignmentValue {
_alignmentValue = alignmentValue;
self.alignment = alignmentValue;
}
- (void)setDistribution:(OAStackViewDistribution)distribution {
if (_distribution == distribution) { return; }
_distribution = distribution;
[self layoutArrangedViews];
}
- (void)setDistributionConstraints {
self.distributionStrategy = [OAStackViewDistributionStrategy strategyWithStackView:self];
[self iterateVisibleViews:^(UIView *view, UIView *previousView) {
[self.distributionStrategy alignView:view afterView:previousView];
}];
[self.distributionStrategy alignView:nil afterView:[self lastVisibleItem]];
}
- (void)setDistributionValue:(NSInteger)distributionValue {
_distributionValue = distributionValue;
self.distribution = distributionValue;
}
@synthesize layoutMargins = _compatibilityLayoutMargins;
- (UIEdgeInsets)layoutMargins {
if (self.nativeLayoutMarginsSupported) {
return super.layoutMargins;
}
return _compatibilityLayoutMargins;
}
- (void)setLayoutMargins:(UIEdgeInsets)layoutMargins {
if (self.nativeLayoutMarginsSupported) {
[super setLayoutMargins:layoutMargins];
} else {
_compatibilityLayoutMargins = layoutMargins;
[self layoutArrangedViews];
}
}
- (void)setLayoutMarginsRelativeArrangement:(BOOL)layoutMarginsRelativeArrangement {
_layoutMarginsRelativeArrangement = layoutMarginsRelativeArrangement;
[self layoutArrangedViews];
}
#pragma mark - Overriden methods
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[self layoutArrangedViews];
}
- (void)layoutMarginsDidChange {
[super layoutMarginsDidChange];
[self layoutArrangedViews];
}
#pragma mark - Adding and removing
- (void)addArrangedSubview:(UIView *)view {
[self insertArrangedSubview:view atIndex:self.subviews.count];
}
- (void)removeArrangedSubview:(UIView *)view {
if (self.subviews.count == 1) {
[self.mutableArrangedSubviews removeObject:view];
[view removeFromSuperview];
return;
}
[self removeViewFromArrangedViews:view permanently:YES];
}
- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex {
[self insertArrangedSubview:view atIndex:stackIndex newItem:YES];
}
- (void)insertArrangedSubview:(UIView *)view atIndex:(NSUInteger)stackIndex newItem:(BOOL)newItem {
id previousView, nextView;
view.translatesAutoresizingMaskIntoConstraints = NO;
BOOL isAppending = stackIndex == self.subviews.count;
if (isAppending) {
//Appending a new item
previousView = [self lastVisibleItem];
nextView = nil;
NSArray<__kindof NSLayoutConstraint *> *constraints = [self lastConstraintAffectingView:self andView:previousView inAxis:self.axis];
if (constraints) {
[self removeConstraints:constraints];
}
if (newItem) {
[self.mutableArrangedSubviews addObject:view];
[self addSubview:view];
}
} else {
//Item insertion
previousView = [self visibleViewBeforeIndex:stackIndex];
nextView = [self visibleViewAfterIndex:newItem ? stackIndex - 1: stackIndex];
NSArray<__kindof NSLayoutConstraint *> *constraints;
BOOL isLastVisibleItem = [self isViewLastItem:previousView excludingItem:view];
BOOL isFirstVisibleView = previousView == nil;
BOOL isOnlyItem = previousView == nil && nextView == nil;
if (isLastVisibleItem) {
constraints = @[[self lastViewConstraint]];
} else if(isOnlyItem) {
constraints = [self constraintsBetweenView:previousView ?: self andView:nextView ?: self inAxis:self.axis];
} else if(isFirstVisibleView) {
constraints = @[[self firstViewConstraint]];
} else {
constraints = [self constraintsBetweenView:previousView ?: self andView:nextView ?: self inAxis:self.axis];
}
[self removeConstraints:constraints];
if (newItem) {
[self.mutableArrangedSubviews insertObject:view atIndex:stackIndex];
[self insertSubview:view atIndex:stackIndex];
}
}
[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 {
NSInteger index = [self.subviews indexOfObject:view];
if (index == NSNotFound) { return; }
id previousView = [self visibleViewBeforeView:view];
id nextView = [self visibleViewAfterView:view];
if (permanently) {
[self.mutableArrangedSubviews removeObject:view];
[view removeFromSuperview];
} else {
NSArray <__kindof NSLayoutConstraint *> *constraint = [self constraintsAffectingView:view];
[self removeConstraints:constraint];
}
if (nextView) {
[self.distributionStrategy alignView:nextView afterView:previousView];
} else if(previousView) {
[self.distributionStrategy alignView:nil afterView:[self lastVisibleItem]];
}
}
#pragma mark - Hide and Unhide
- (void)hideView:(UIView*)view {
[self removeViewFromArrangedViews:view permanently:NO];
}
- (void)unHideView:(UIView*)view {
NSInteger index = [self.subviews indexOfObject:view];
[self insertArrangedSubview:view atIndex:index newItem:NO];
}
#pragma mark - Align View
- (void)layoutArrangedViews {
NSMutableArray *constraints = [NSMutableArray array];
[constraints addObjectsFromArray:self.alignmentStrategy.addedConstraints];
[constraints addObjectsFromArray:self.distributionStrategy.addedConstraints];
[self removeConstraints:constraints];
[self setAlignmentConstraints];
[self setDistributionConstraints];
}
- (void)addViewsAsSubviews:(NSArray*)views {
for (UIView *view in views) {
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
}
}
@end
@implementation OAStackViewProxy
@end
#pragma mark - Runtime Injection
// Constructors are called after all classes have been loaded.
__attribute__((constructor)) static void OAStackViewPatchEntry(void) {
if (objc_getClass("UIStackView")) {
return;
}
if (objc_getClass("OAStackViewDisableForwardToUIStackViewSentinel")) {
return;
}
Class class = objc_allocateClassPair(OAStackView.class, "UIStackView", 0);
if (class) {
objc_registerClassPair(class);
}
}