forked from smileyborg/UIView-AutoLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALViewController.m
More file actions
433 lines (364 loc) · 16.5 KB
/
ALViewController.m
File metadata and controls
433 lines (364 loc) · 16.5 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
// ALViewController.m
// Example
//
// Copyright (c) 2013 Tyler Fox
// https://github.com/smileyborg/UIView-AutoLayout
//
#import "ALViewController.h"
typedef NS_ENUM(NSInteger, ExampleConstraintDemo) {
ExampleConstraintDemoReset = 0,
ExampleConstraintDemo1,
ExampleConstraintDemo2,
ExampleConstraintDemo3,
ExampleConstraintDemo4,
ExampleConstraintDemo5,
ExampleConstraintDemo6,
ExampleConstraintDemo7,
ExampleConstraintDemoCount
};
@interface ALViewController ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) UIView *redView;
@property (nonatomic, strong) UIView *yellowView;
@property (nonatomic, strong) UIView *greenView;
@property (nonatomic, strong) UILabel *orangeView;
@property (nonatomic, assign) ExampleConstraintDemo constraintDemo;
@property (nonatomic, assign) BOOL isAnimatingDemo3;
@property (nonatomic, strong) NSLayoutConstraint *demo3BlueBottomInset;
@property (nonatomic, strong) NSLayoutConstraint *demo3BlueRightInset;
@property (nonatomic, strong) NSLayoutConstraint *demo3RedSizeConstraint;
@property (nonatomic, strong) NSLayoutConstraint *demo3GreenPinConstraint;
@end
@implementation ALViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupViews];
// Start off by resetting and advancing to the first demo
self.constraintDemo = ExampleConstraintDemoReset;
[self nextDemo];
// Change the demo when the screen is tapped
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextDemo)]];
}
- (void)setupViews
{
[self.view addSubview:self.containerView];
[self.containerView addSubview:self.blueView];
[self.containerView addSubview:self.redView];
[self.containerView addSubview:self.yellowView];
[self.containerView addSubview:self.greenView];
[self.containerView addSubview:self.orangeView];
}
- (void)updateViewConstraints
{
[super updateViewConstraints];
[self setupConstraintsForCurrentDemo];
}
/**
Demonstrates:
- Setting a view to a fixed width
- Matching the widths of subviews
- Distributing subviews vertically with a fixed height
*/
- (void)setupDemo1
{
NSArray *subviews = @[self.blueView, self.redView, self.yellowView, self.greenView, self.orangeView];
[self.blueView autoSetDimension:ALDimensionWidth toSize:80.0f];
[subviews autoMatchViewsDimension:ALDimensionWidth];
[self.orangeView autoAlignAxisToSuperviewAxis:ALAxisVertical];
[subviews autoDistributeViewsAlongAxis:ALAxisVertical withFixedSize:30.0f insetSpacing:YES alignment:NSLayoutFormatAlignAllCenterX];
}
/**
Demonstrates:
- Matching a view's width to its height
- Matching the heights of subviews
- Distributing subviews horizontally with fixed spacing
*/
- (void)setupDemo2
{
NSArray *subviews = @[self.blueView, self.redView, self.yellowView, self.greenView, self.orangeView];
[self.blueView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:self.blueView];
[subviews autoMatchViewsDimension:ALDimensionHeight];
[self.orangeView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[subviews autoDistributeViewsAlongAxis:ALAxisHorizontal withFixedSpacing:10.0f insetSpacing:NO alignment:NSLayoutFormatAlignAllCenterY];
}
/**
Demonstrates:
- Animation with constraints
- Setting a priority less than required
- Complicated interaction of various constraints
*/
- (void)setupDemo3
{
[self.orangeView autoSetDimensionsToSize:CGSizeZero]; // orange view not used in this demo; this prevents it from taking on its intrinsic content size
[UIView autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
[self.blueView autoSetDimensionsToSize:CGSizeMake(60.0f, 80.0f)];
}];
[self.blueView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.blueView.superview withOffset:-80.0f relation:NSLayoutRelationLessThanOrEqual];
self.demo3BlueBottomInset = [self.blueView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:50.0f];
self.demo3BlueRightInset = [self.blueView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10.0f];
[self.redView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.blueView];
self.demo3RedSizeConstraint = [self.redView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.blueView withOffset:-40.0f];
[self.redView autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.blueView];
[self.blueView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.redView withOffset:30.0f];
self.demo3GreenPinConstraint = [self.greenView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.redView];
[self.greenView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.redView withOffset:-50.0f];
[self.greenView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionHeight ofView:self.redView];
[self.greenView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:self.blueView];
[self.view layoutIfNeeded];
if (self.isAnimatingDemo3 == NO) {
// Begin animation on next run loop after initial layout has been calculated
double delayInSeconds = 0.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
self.isAnimatingDemo3 = YES;
[self animateDemo3Constraints];
});
}
}
/**
Runs 1 cycle of the animation for demo 3.
Notes for animating constraints:
- If modifying the constant of a constraint, just set the existing constraint's constant to the new value
- If modifying any other constraint properties, must remove the old constraint and add a new one with the new values
- Must call layoutIfNeeded at end of animation block
*/
- (void)animateDemo3Constraints
{
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.demo3BlueBottomInset.constant = -10.0f;
self.demo3BlueRightInset.constant = -50.0f;
self.demo3RedSizeConstraint.constant = 10.0f;
[self.demo3GreenPinConstraint autoRemove];
self.demo3GreenPinConstraint = [self.greenView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.blueView];
[self.view layoutIfNeeded];
}
completion:^(BOOL finished){
if (self.constraintDemo != ExampleConstraintDemo3) {
self.isAnimatingDemo3 = NO;
return;
}
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
self.demo3BlueBottomInset.constant = -50.0f;
self.demo3BlueRightInset.constant = -10.0f;
self.demo3RedSizeConstraint.constant = -40.0f;
[self.demo3GreenPinConstraint autoRemove];
self.demo3GreenPinConstraint = [self.greenView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.redView];
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
if (self.constraintDemo == ExampleConstraintDemo3) {
// Loop the animation while viewing the same demo
[self animateDemo3Constraints];
} else {
self.isAnimatingDemo3 = NO;
}
}];
}];
}
/**
Demonstrates:
- Achieving a common layout scenario for content (e.g. an image view, title label, and body text)
- Matching the widths of two views using a multiplier
- Pinning views to each other and to the superview to maintain padding and insets
*/
- (void)setupDemo4
{
[self.redView autoSetDimension:ALDimensionHeight toSize:44.0f];
[self.blueView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.redView];
[self.redView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20.0f];
[self.redView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:20.0f];
[self.blueView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:20.0f];
[self.blueView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:20.0f];
[self.blueView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.redView withOffset:10.0f];
[self.blueView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.redView withMultiplier:3.0f];
[self.orangeView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.blueView withOffset:20.0f];
[self.orangeView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.redView];
[self.orangeView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.blueView];
[self.orangeView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:20.0f];
}
/**
Demonstrates:
- Everything from Demo 4, and...
- Using leading/trailing edge attributes instead of left/right
(Change language from English to Arabic to see the difference.)
*/
- (void)setupDemo5
{
[self.redView autoSetDimension:ALDimensionHeight toSize:44.0f];
[self.blueView autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.redView];
[self.redView autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:20.0f];
[self.redView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:20.0f];
[self.blueView autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:20.0f];
[self.blueView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:20.0f];
[self.blueView autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:self.redView withOffset:10.0f];
[self.blueView autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.redView withMultiplier:3.0f];
[self.orangeView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.blueView withOffset:20.0f];
[self.orangeView autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:self.redView withOffset:20.0];
[self.orangeView autoPinEdge:ALEdgeTrailing toEdge:ALEdgeTrailing ofView:self.blueView withOffset:-10.0];
[self.orangeView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:20.0f];
}
/**
Demonstrates:
- Looping over subviews to apply constraints between them
- Setting a priority less than required for specific constraints
- Specifying an inequality constraint that competes with the lower priority constraints
--> the orange view will maintain at least 10 points of spacing to the bottom of its superview (required constraint),
and this may require reducing its height (breaking the lower priority constraint)
*/
- (void)setupDemo6
{
[self.blueView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.0f];
[self.blueView autoSetDimensionsToSize:CGSizeMake(25.0f, 10.0f)];
[self.blueView autoAlignAxisToSuperviewAxis:ALAxisVertical];
NSArray *subviews = @[self.blueView, self.redView, self.yellowView, self.greenView, self.orangeView];
[subviews autoAlignViewsToAxis:ALAxisVertical];
UIView *previousView = nil;
for (UIView *view in subviews) {
if (previousView) {
[view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:previousView withOffset:10.0f];
// The orange view will be allowed to change its size if it conflicts with a required constraint
UILayoutPriority priority = (view == self.orangeView) ? UILayoutPriorityDefaultHigh + 1 : UILayoutPriorityRequired;
[UIView autoSetPriority:priority forConstraints:^{
[view autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:previousView withMultiplier:1.5f];
[view autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:previousView withMultiplier:2.0f];
}];
}
previousView = view;
}
[self.orangeView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.containerView withOffset:-10.0f relation:NSLayoutRelationLessThanOrEqual];
}
/**
Demonstrates:
- Applying a constraint across different types of attributes
*/
- (void)setupDemo7
{
[self.redView autoCenterInSuperview];
[self.redView autoSetDimensionsToSize:CGSizeMake(100.0f, 250.0f)];
[self.orangeView autoSetDimensionsToSize:CGSizeMake(50.0f, 50.0f)];
[self.orangeView autoConstrainAttribute:ALAxisHorizontal toAttribute:ALEdgeTop ofView:self.redView];
}
#pragma mark Private Helper Methods
/**
Switches to the next demo in the sequence.
Removes all constraints, then calls the next demo's setup method.
*/
- (void)setupConstraintsForCurrentDemo
{
if (self.constraintDemo >= ExampleConstraintDemoCount) {
// Return to the first demo after the last one
self.constraintDemo = ExampleConstraintDemo1;
}
// WARNING: Be sure to read the documentation on the below method - it can cause major performance issues!
[self.containerView autoRemoveConstraintsAffectingViewAndSubviews];
[self.containerView autoPinToTopLayoutGuideOfViewController:self withInset:10.0f];
[self.containerView autoPinToBottomLayoutGuideOfViewController:self withInset:10.0f];
[self.containerView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10.0f];
[self.containerView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10.0f];
switch (self.constraintDemo) {
case ExampleConstraintDemo1:
[self setupDemo1];
break;
case ExampleConstraintDemo2:
[self setupDemo2];
break;
case ExampleConstraintDemo3:
[self setupDemo3];
break;
case ExampleConstraintDemo4:
[self setupDemo4];
break;
case ExampleConstraintDemo5:
[self setupDemo5];
break;
case ExampleConstraintDemo6:
[self setupDemo6];
break;
case ExampleConstraintDemo7:
[self setupDemo7];
break;
default:
self.constraintDemo = ExampleConstraintDemoReset;
break;
}
}
/**
Advances to the next demo and flags the view for a constraint update.
*/
- (void)nextDemo
{
self.constraintDemo++;
[self.view setNeedsUpdateConstraints];
}
#pragma mark Property Accessors
- (UIView *)containerView
{
if (!_containerView) {
_containerView = [UIView newAutoLayoutView];
_containerView.backgroundColor = [UIColor blackColor];
}
return _containerView;
}
- (UIView *)blueView
{
if (!_blueView) {
_blueView = [[UIView alloc] initForAutoLayout];
_blueView.backgroundColor = [UIColor blueColor];
}
return _blueView;
}
- (UIView *)redView
{
if (!_redView) {
_redView = [[UIView alloc] initForAutoLayout];
_redView.backgroundColor = [UIColor redColor];
}
return _redView;
}
- (UIView *)yellowView
{
if (!_yellowView) {
_yellowView = [[UIView alloc] initForAutoLayout];
_yellowView.backgroundColor = [UIColor yellowColor];
}
return _yellowView;
}
- (UIView *)greenView
{
if (!_greenView) {
_greenView = [[UIView alloc] initForAutoLayout];
_greenView.backgroundColor = [UIColor greenColor];
}
return _greenView;
}
- (UILabel *)orangeView
{
if (!_orangeView) {
_orangeView = [[UILabel alloc] initForAutoLayout];
_orangeView.backgroundColor = [UIColor orangeColor];
_orangeView.numberOfLines = 0;
_orangeView.font = [UIFont systemFontOfSize:10.0f];
_orangeView.textColor = [UIColor whiteColor];
_orangeView.textAlignment = NSTextAlignmentCenter;
_orangeView.text = NSLocalizedString(@"Lorem ipsum", nil);
}
return _orangeView;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
@end