-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathOAStackView+Constraint.m
More file actions
170 lines (135 loc) · 6.22 KB
/
OAStackView+Constraint.m
File metadata and controls
170 lines (135 loc) · 6.22 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
//
// OAStackView+Constraint.m
// Pods
//
// Created by Omar Abdelhafith on 14/06/2015.
//
//
#import "OAStackView+Constraint.h"
@implementation OAStackView (Constraint)
- (NSArray*)constraintsBetweenView:(UIView*)firstView andView:(UIView*)otherView
inAxis:(UILayoutConstraintAxis)axis includeReversed:(BOOL)includeReversed {
NSMutableArray *arr = [@[] mutableCopy];
for (NSLayoutConstraint *constraint in self.constraints) {
BOOL viewMatches = (firstView == constraint.firstItem && otherView == constraint.secondItem);
if (includeReversed) {
viewMatches = viewMatches || (firstView == constraint.secondItem && otherView == constraint.firstItem);
}
BOOL isCorrectAxis = [self isConstraint:constraint affectingAxis:axis];
if (viewMatches && isCorrectAxis) {
[arr addObject:constraint];
}
}
return arr;
}
- (NSArray*)firstConstraintAffectingView:(UIView*)superView andView:(UIView*)childView inAxis:(UILayoutConstraintAxis)axis {
return [self constraintAffectingView:superView andView:childView inAxis:axis isLast:NO];
}
- (NSArray*)lastConstraintAffectingView:(UIView*)superView andView:(UIView*)childView inAxis:(UILayoutConstraintAxis)axis {
return [self constraintAffectingView:superView andView:childView inAxis:axis isLast:YES];
}
- (NSArray*)constraintAffectingView:(UIView*)superView andView:(UIView*)childView inAxis:(UILayoutConstraintAxis)axis isLast:(BOOL)isLast {
if (childView == nil || superView == nil) {
return nil;
}
NSMutableArray *arr = [@[] mutableCopy];
for (NSLayoutConstraint *constraint in self.constraints) {
BOOL isBetweenViews = [self isConstraint:constraint betweenView:superView andView:childView];
if (!isBetweenViews) { continue; }
BOOL isOnAxis = [self isConstraint:constraint affectingAxis:axis];
if (!isOnAxis) { continue; }
BOOL matches = NO;
if (axis == UILayoutConstraintAxisHorizontal) {
if (isLast) {
matches =
(constraint.firstAttribute == NSLayoutAttributeRight || constraint.firstAttribute == NSLayoutAttributeTrailing) &&
(constraint.secondAttribute == NSLayoutAttributeRight || constraint.secondAttribute == NSLayoutAttributeTrailing);
} else {
matches =
(constraint.firstAttribute == NSLayoutAttributeLeft || constraint.firstAttribute == NSLayoutAttributeLeading) &&
(constraint.secondAttribute == NSLayoutAttributeLeft || constraint.secondAttribute == NSLayoutAttributeLeading);
}
} else {
if (isLast) {
matches =
(constraint.firstAttribute == NSLayoutAttributeBottom) &&
(constraint.secondAttribute == NSLayoutAttributeBottom);
} else {
matches =
(constraint.firstAttribute == NSLayoutAttributeTop) &&
(constraint.secondAttribute == NSLayoutAttributeTop);
}
}
if (matches) {
[arr addObject:constraint];
}
}
return arr;
}
- (BOOL)isConstraint:(NSLayoutConstraint*)constraint betweenView:(UIView*)firstView andView:(UIView*)secondView {
return (firstView == constraint.firstItem && secondView == constraint.secondItem) ||
(firstView == constraint.secondItem && secondView == constraint.firstItem);
}
- (NSArray*)constraintsBetweenView:(UIView*)firstView andView:(UIView*)otherView inAxis:(UILayoutConstraintAxis)axis {
return [self constraintsBetweenView:firstView andView:otherView inAxis:axis includeReversed:YES];
}
- (NSArray*)constraintsAffectingView:(UIView*)view {
NSMutableArray *arr = [@[] mutableCopy];
[arr addObjectsFromArray:[self constraintsAffectingView:view inAxis:UILayoutConstraintAxisVertical]];
[arr addObjectsFromArray:[self constraintsAffectingView:view inAxis:UILayoutConstraintAxisHorizontal]];
return arr;
}
- (NSArray*)constraintsAffectingView:(UIView*)view inAxis:(UILayoutConstraintAxis)axis {
NSMutableArray *arr = [@[] mutableCopy];
for (NSLayoutConstraint *constraint in self.constraints) {
BOOL viewMatches = (view == constraint.firstItem || view == constraint.secondItem);
BOOL constraintAffectingAxis = [self isConstraint:constraint affectingAxis:axis];
if (viewMatches && constraintAffectingAxis) {
[arr addObject:constraint];
}
}
return arr;
}
- (void)removeDecendentConstraints {
for (NSInteger i = self.constraints.count - 1; i >= 0 ; i--) {
NSLayoutConstraint *constraint = self.constraints[i];
if ([self.subviews containsObject:constraint.firstItem] ||
[self.subviews containsObject:constraint.secondItem]) {
[self removeConstraint:constraint];
}
}
}
- (BOOL)isConstraint:(NSLayoutConstraint*)constraint affectingAxis:(UILayoutConstraintAxis)axis {
return [self isConstraintAttribute:constraint.firstAttribute affectingAxis:axis] ||
[self isConstraintAttribute:constraint.secondAttribute affectingAxis:axis];
}
- (BOOL)isConstraintAttribute:(NSLayoutAttribute)attribute affectingAxis:(UILayoutConstraintAxis)axis {
switch (axis) {
case UILayoutConstraintAxisHorizontal:
return attribute == NSLayoutAttributeLeft || attribute == NSLayoutAttributeRight ||
attribute == NSLayoutAttributeLeading || attribute == NSLayoutAttributeTrailing ||
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 == 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;
}
}
@end