-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathOAStackView+Traversal.m
More file actions
104 lines (80 loc) · 2.98 KB
/
OAStackView+Traversal.m
File metadata and controls
104 lines (80 loc) · 2.98 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
//
// OAStackView+Traversal.m
// Pods
//
// Created by Omar Abdelhafith on 15/06/2015.
//
//
#import "OAStackView+Traversal.h"
#import "OAStackView_ArrangedSubviews.h"
@implementation OAStackView (Traversal)
- (UIView*)arrangedSubviewBeforeView:(UIView*)view {
NSInteger index = [self indexInArrangedSubviewsOfObject:view];
if (index == NSNotFound) { return nil; }
return [self arrangedSubviewBeforeIndex:index];
}
- (UIView*)arrangedSubviewAfterView:(UIView*)view {
NSInteger index = [self indexInArrangedSubviewsOfObject:view];
if (index == NSNotFound) { return nil; }
return [self arrangedSubviewAfterIndex:index];
}
- (UIView*)arrangedSubviewAfterIndex:(NSInteger)index {
if ((index + 1) < [self countOfArrangedSubviews]) {
return [self objectInArrangedSubviewsAtIndex:index+1];
}
return nil;
}
- (UIView*)arrangedSubviewBeforeIndex:(NSInteger)index {
if (index > 0) {
return [self objectInArrangedSubviewsAtIndex:index-1];
}
return nil;
}
- (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;
}
}
- (NSLayoutConstraint*)lastViewConstraint {
for (NSLayoutConstraint *constraint in self.constraints) {
if (self.axis == UILayoutConstraintAxisVertical) {
if ( (constraint.firstItem == self && constraint.firstAttribute == NSLayoutAttributeBottom) ||
(constraint.secondItem == self && constraint.secondAttribute == NSLayoutAttributeBottom)) {
return constraint;
}
} else {
if ( (constraint.firstItem == self && constraint.firstAttribute == NSLayoutAttributeTrailing) ||
(constraint.secondItem == self && constraint.secondAttribute == NSLayoutAttributeTrailing)) {
return constraint;
}
}
}
return nil;
}
- (NSLayoutConstraint*)firstViewConstraint {
for (NSLayoutConstraint *constraint in self.constraints) {
if (self.axis == UILayoutConstraintAxisVertical) {
if ( (constraint.firstItem == self && constraint.firstAttribute == NSLayoutAttributeTop) ||
(constraint.secondItem == self && constraint.secondAttribute == NSLayoutAttributeTop)) {
return constraint;
}
} else {
if ( (constraint.firstItem == self && constraint.firstAttribute == NSLayoutAttributeLeading) ||
(constraint.secondItem == self && constraint.secondAttribute == NSLayoutAttributeLeading)) {
return constraint;
}
}
}
return nil;
}
- (BOOL)isViewLastItem:(UIView*)view excludingItem:(UIView*)excludingItem {
NSArray *visible = [self arrangedSubviews];
NSInteger index = [visible indexOfObject:view];
NSInteger exclutedIndex = [visible indexOfObject:excludingItem];
return index == visible.count - 1 ||
(exclutedIndex == visible.count - 1 && index == visible.count - 2);
}
@end