forked from jrturton/UIView-Autolayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+AutoLayout.m
266 lines (220 loc) · 11.4 KB
/
UIView+AutoLayout.m
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
//
// UIView+AutoLayout.m
// CollectionTableGrid
//
// Created by Richard Turton on 18/10/2012.
// Copyright (c) 2012 Richard Turton. All rights reserved.
//
#import "UIView+AutoLayout.h"
@implementation UIView (AutoLayout)
+(id)autoLayoutView
{
UIView *viewToReturn = [self new];
viewToReturn.translatesAutoresizingMaskIntoConstraints = NO;
return viewToReturn;
}
-(NSArray *)centerInView:(UIView*)superview
{
NSMutableArray *constraints = [NSMutableArray new];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
[superview addConstraints:constraints];
return [constraints copy];
}
-(NSLayoutConstraint *)centerInContainerOnAxis:(NSLayoutAttribute)axis
{
NSParameterAssert(axis == NSLayoutAttributeCenterX || axis == NSLayoutAttributeCenterY);
UIView *superview = self.superview;
NSParameterAssert(superview);
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:axis relatedBy:NSLayoutRelationEqual toItem:superview attribute:axis multiplier:1.0 constant:0.0];
[superview addConstraint:constraint];
return constraint;
}
-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView;
{
NSParameterAssert(peerView);
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:peerView attribute:attribute multiplier:1.0 constant:0.0];
[superview addConstraint:constraint];
return constraint;
}
-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView withMultiplier:(CGFloat)multiplier;
{
NSParameterAssert(peerView);
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:peerView attribute:attribute multiplier:multiplier constant:0.0];
[superview addConstraint:constraint];
return constraint;
}
-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView withConstant:(CGFloat)constant;
{
NSParameterAssert(peerView);
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:peerView attribute:attribute multiplier:1.0 constant:constant];
[superview addConstraint:constraint];
return constraint;
}
-(NSArray*)pinToSuperviewEdges:(JRTViewPinEdges)edges inset:(CGFloat)inset
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't pin to a superview if no superview exists");
NSMutableArray *constraints = [NSMutableArray new];
if (edges & JRTViewPinTopEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:inset]];
}
if (edges & JRTViewPinLeftEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:inset]];
}
if (edges & JRTViewPinRightEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-inset]];
}
if (edges & JRTViewPinBottomEdge)
{
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-inset]];
}
[superview addConstraints:constraints];
return [constraints copy];
}
-(NSArray*)pinToSuperviewEdgesWithInset:(UIEdgeInsets)insets
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't pin to a superview if no superview exists");
NSMutableArray *constraints = [NSMutableArray new];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:JRTViewPinTopEdge inset:insets.top]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:JRTViewPinLeftEdge inset:insets.left]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:JRTViewPinBottomEdge inset:insets.bottom]];
[constraints addObjectsFromArray:[self pinToSuperviewEdges:JRTViewPinRightEdge inset:insets.right]];
return [constraints copy];
}
-(NSLayoutConstraint *)pinEdge:(NSLayoutAttribute)edge toEdge:(NSLayoutAttribute)toEdge ofView:(UIView*)peerView
{
return [self pinEdge:edge toEdge:toEdge ofView:peerView inset:0.0];
}
-(NSLayoutConstraint *)pinEdge:(NSLayoutAttribute)edge toEdge:(NSLayoutAttribute)toEdge ofView:(UIView *)peerView inset:(CGFloat)inset
{
UIView *superview = [self commonSuperviewWithView:peerView];
NSAssert(superview,@"Can't create constraints without a common superview");
NSAssert (edge >= NSLayoutAttributeLeft && edge <= NSLayoutAttributeBottom,@"Edge parameter is not an edge");
NSAssert (toEdge >= NSLayoutAttributeLeft && toEdge <= NSLayoutAttributeBottom,@"Edge parameter is not an edge");
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:edge relatedBy:NSLayoutRelationEqual toItem:peerView attribute:toEdge multiplier:1.0 constant:inset];
[superview addConstraint:constraint];
return constraint;
}
-(NSArray *)constrainToSize:(CGSize)size
{
NSMutableArray *constraints = [NSMutableArray new];
if (size.width)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:size.width]];
if (size.height)
[constraints addObject:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:0 constant:size.height]];
[self addConstraints:constraints];
return [constraints copy];
}
-(void)pinPointAtX:(NSLayoutAttribute)x Y:(NSLayoutAttribute)y toPoint:(CGPoint)point
{
UIView *superview = self.superview;
NSAssert(superview,@"Can't create constraints without a superview");
// Valid X positions are Left, Center, Right and Not An Attribute
BOOL xValid = (x == NSLayoutAttributeLeft || x == NSLayoutAttributeCenterX || x == NSLayoutAttributeRight || x == NSLayoutAttributeNotAnAttribute);
// Valid Y positions are Top, Center, Baseline, Bottom and Not An Attribute
BOOL yValid = (y == NSLayoutAttributeTop || y == NSLayoutAttributeCenterY || y == NSLayoutAttributeBaseline || y == NSLayoutAttributeBottom || y == NSLayoutAttributeNotAnAttribute);
NSAssert (xValid && yValid,@"Invalid positions for creating constraints");
if (x != NSLayoutAttributeNotAnAttribute)
[superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:x relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:point.x]];
if (y != NSLayoutAttributeNotAnAttribute)
[superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:y relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:point.y]];
}
-(void)spaceViews:(NSArray*)views onAxis:(UILayoutConstraintAxis)axis withSpacing:(CGFloat)spacing withInset:(CGFloat)inset alignmentOptions:(NSLayoutFormatOptions)options;
{
NSAssert([views count] > 1,@"Can only distribute 2 or more views");
NSString *direction = nil;
switch (axis) {
case UILayoutConstraintAxisHorizontal:
direction = @"H:";
break;
case UILayoutConstraintAxisVertical:
direction = @"V:";
break;
default:
return;
}
UIView *previousView = nil;
NSDictionary *metrics = @{@"spacing":@(spacing),@"inset":@(inset)};
NSString *vfl = nil;
for (UIView *view in views)
{
vfl = nil;
NSDictionary *views = nil;
if (previousView)
{
vfl = [NSString stringWithFormat:@"%@[previousView(==view)]-spacing-[view]",direction];
views = NSDictionaryOfVariableBindings(previousView,view);
}
else
{
vfl = [NSString stringWithFormat:@"%@|-inset-[view]",direction];
views = NSDictionaryOfVariableBindings(view);
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:views]];
previousView = view;
}
vfl = [NSString stringWithFormat:@"%@[previousView]-inset-|",direction];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:NSDictionaryOfVariableBindings(previousView)]];
}
-(void)spaceViews:(NSArray*)views onAxis:(UILayoutConstraintAxis)axis withSpacing:(CGFloat)spacing alignmentOptions:(NSLayoutFormatOptions)options;
{
NSAssert([views count] > 1,@"Can only distribute 2 or more views");
NSString *direction = nil;
switch (axis) {
case UILayoutConstraintAxisHorizontal:
direction = @"H:";
break;
case UILayoutConstraintAxisVertical:
direction = @"V:";
break;
default:
return;
}
UIView *previousView = nil;
NSDictionary *metrics = @{@"spacing":@(spacing)};
NSString *vfl = nil;
for (UIView *view in views)
{
vfl = nil;
NSDictionary *views = nil;
if (previousView)
{
vfl = [NSString stringWithFormat:@"%@[previousView(==view)]-spacing-[view]",direction];
views = NSDictionaryOfVariableBindings(previousView,view);
}
else
{
vfl = [NSString stringWithFormat:@"%@|-spacing-[view]",direction];
views = NSDictionaryOfVariableBindings(view);
}
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:views]];
previousView = view;
}
vfl = [NSString stringWithFormat:@"%@[previousView]-spacing-|",direction];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:options metrics:metrics views:NSDictionaryOfVariableBindings(previousView)]];
}
-(UIView*)commonSuperviewWithView:(UIView*)peerView
{
UIView *commonSuperview = nil;
UIView *startView = self;
do {
if ([peerView isDescendantOfView:startView])
{
commonSuperview = startView;
}
startView = startView.superview;
} while (startView && !commonSuperview);
return commonSuperview;
}
@end