-
Notifications
You must be signed in to change notification settings - Fork 57
/
AutoLayoutConstraintsSpec.m
659 lines (479 loc) · 31.2 KB
/
AutoLayoutConstraintsSpec.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//
// AutoLayoutTests.m
// AutoLayoutTests
//
// Created by James Frost on 20/05/2014.
// Copyright (c) 2014 James Frost. All rights reserved.
//
#import <Specta/Specta.h>
#import <XCTest/XCTest.h>
#define EXP_SHORTHAND
#import <Expecta/Expecta.h>
#import "EXPMatchers+haveConstraint.h"
#import "EXPMatchers+haveFrame.h"
#import "UIView+AutoLayout.h"
#define JRTCenterConstraintWithAxis(axis) [NSLayoutConstraint constraintWithItem:subview attribute:axis relatedBy:NSLayoutRelationEqual toItem:superview attribute:axis multiplier:1.0f constant:0]
#define JRTSizeConstraintWithAttributeAndSize(axis, size) [NSLayoutConstraint constraintWithItem:subview attribute:axis relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:size]
#define JRTSizeConstraintWithAttributeSizeAndRelation(axis, size, relation) [NSLayoutConstraint constraintWithItem:subview attribute:axis relatedBy:relation toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:size]
#define JRTEdgeConstraintWithEdgeAndEqualInset(edge, inset) [NSLayoutConstraint constraintWithItem:subview attribute:edge relatedBy:NSLayoutRelationEqual toItem:superview attribute:edge multiplier:1.0 constant:inset]
SpecBegin(AutoLayoutConstraints)
describe(@"AutoLayout constraints", ^{
__block UIView *superview = nil;
__block UIView *subview = nil;
context(@"when initializing views", ^{
it(@"returns a view without translated autoresizing masks", ^{
UIView *view = [UIView autoLayoutView];
expect(view.translatesAutoresizingMaskIntoConstraints).to.beFalsy();
});
});
context(@"when centering views", ^{
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
subview = [UIView autoLayoutView];
[superview addSubview:subview];
[subview constrainToSize:CGSizeMake(100, 100)];
});
it(@"centers a view horizontally", ^{
[subview centerInContainerOnAxis:NSLayoutAttributeCenterX];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterX));
expect(CGRectGetMinX(subview.frame)).to.equal(100);
});
it(@"centers a view vertically", ^{
[subview centerInContainerOnAxis:NSLayoutAttributeCenterY];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterY));
expect(CGRectGetMinY(subview.frame)).to.equal(100);
});
it(@"centers a view on both axes", ^{
[subview centerInContainer];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterX));
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterY));
expect(CGRectGetMinY(subview.frame)).to.equal(100);
});
it(@"centers a view within another view horizontally", ^{
[subview centerInView:superview onAxis:NSLayoutAttributeCenterX];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterX));
expect(superview).toNot.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterY));
expect(subview.center.x).to.equal(150);
});
it(@"centers a view within another view vertically", ^{
[subview centerInView:superview onAxis:NSLayoutAttributeCenterY];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterY));
expect(superview).toNot.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterX));
expect(subview.center.y).to.equal(150);
});
it(@"centers a view within another view", ^{
[subview centerInView:superview];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterX));
expect(superview).to.haveConstraint(JRTCenterConstraintWithAxis(NSLayoutAttributeCenterY));
expect(subview.center).to.equal(CGPointMake(150, 150));
});
});
context(@"when constraining size", ^{
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
subview = [UIView autoLayoutView];
[superview addSubview:subview];
});
it(@"constrains just the width", ^{
[subview constrainToSize:CGSizeMake(100, 0)];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeWidth, 100));
expect(subview.constraints).to.haveCountOf(1);
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(0);
});
it(@"constrains just the height", ^{
[subview constrainToSize:CGSizeMake(0, 100)];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeHeight, 100));
expect(subview.constraints).to.haveCountOf(1);
expect(CGRectGetWidth(subview.bounds)).will.equal(0);
expect(CGRectGetHeight(subview.bounds)).will.equal(100);
});
it(@"constrains both width and height", ^{
[subview constrainToSize:CGSizeMake(100, 100)];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeWidth, 100));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeHeight, 100));
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(100);
});
it(@"constrains to a minimum size", ^{
[subview constrainToMinimumSize:CGSizeMake(100, 150)];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeWidth, 100, NSLayoutRelationGreaterThanOrEqual));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeHeight, 150, NSLayoutRelationGreaterThanOrEqual));
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(150);
});
it(@"constrains to a maximum size", ^{
[subview constrainToMaximumSize:CGSizeMake(100, 150)];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeWidth, 100, NSLayoutRelationLessThanOrEqual));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeHeight, 150, NSLayoutRelationLessThanOrEqual));
expect(CGRectGetWidth(subview.bounds)).will.equal(0);
expect(CGRectGetHeight(subview.bounds)).will.equal(0);
});
it(@"constrains to both a maximum and minimum size", ^{
[subview constrainToMinimumSize:CGSizeMake(100, 150) maximumSize:CGSizeMake(200, 300)];
[superview layoutIfNeeded];
// minimum sizes
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeWidth, 100, NSLayoutRelationGreaterThanOrEqual));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeHeight, 150, NSLayoutRelationGreaterThanOrEqual));
// maximum sizes
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeWidth, 200, NSLayoutRelationLessThanOrEqual));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeSizeAndRelation(NSLayoutAttributeHeight, 300, NSLayoutRelationLessThanOrEqual));
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(150);
});
});
context(@"when constraining width and height", ^{
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
subview = [UIView autoLayoutView];
[superview addSubview:subview];
});
it(@"constrains the width", ^{
[subview constrainToWidth:100];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeWidth, 100));
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(0);
});
it(@"constrains the height", ^{
[subview constrainToHeight:100];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeHeight, 100));
expect(CGRectGetWidth(subview.bounds)).will.equal(0);
expect(CGRectGetHeight(subview.bounds)).will.equal(100);
});
it(@"constrains both width and height", ^{
[subview constrainToWidth:100];
[subview constrainToHeight:100];
[superview layoutIfNeeded];
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeWidth, 100));
expect(subview).to.haveConstraint(JRTSizeConstraintWithAttributeAndSize(NSLayoutAttributeHeight, 100));
expect(CGRectGetWidth(subview.bounds)).will.equal(100);
expect(CGRectGetHeight(subview.bounds)).will.equal(100);
});
});
context(@"when pinning to a superview's edges", ^{
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
subview = [UIView autoLayoutView];
[superview addSubview:subview];
});
it(@"pins to just the top edge", ^{
[subview pinToSuperviewEdges:JRTViewPinTopEdge inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeTop, 0));
expect(superview.constraints).to.haveCountOf(1);
expect(CGRectGetMinY(subview.frame)).to.equal(CGRectGetMinY(superview.frame));
});
it(@"pins to just the right edge", ^{
[subview pinToSuperviewEdges:JRTViewPinRightEdge inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeRight, 0));
expect(superview.constraints).to.haveCountOf(1);
expect(CGRectGetMaxX(subview.frame)).to.equal(CGRectGetMaxX(superview.frame));
});
it(@"pins to just the bottom edge", ^{
[subview pinToSuperviewEdges:JRTViewPinBottomEdge inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeBottom, 0));
expect(superview.constraints).to.haveCountOf(1);
expect(CGRectGetMaxY(subview.frame)).to.equal(CGRectGetMaxY(superview.frame));
});
it(@"pins to just the left edge", ^{
[subview pinToSuperviewEdges:JRTViewPinLeftEdge inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeLeft, 0));
expect(superview.constraints).to.haveCountOf(1);
expect(CGRectGetMinX(subview.frame)).to.equal(CGRectGetMinX(superview.frame));
});
it(@"pins to a bitmask of edges", ^{
[subview pinToSuperviewEdges:JRTViewPinTopEdge|JRTViewPinLeftEdge|JRTViewPinBottomEdge|JRTViewPinRightEdge inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeTop, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeLeft, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeBottom, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeRight, 0));
expect(superview.constraints).to.haveCountOf(4);
expect(CGRectGetMinY(subview.frame)).to.equal(CGRectGetMinY(superview.frame));
expect(CGRectGetMinX(subview.frame)).to.equal(CGRectGetMinX(superview.frame));
expect(CGRectGetMaxY(subview.frame)).to.equal(CGRectGetMaxY(superview.frame));
expect(CGRectGetMaxX(subview.frame)).to.equal(CGRectGetMaxX(superview.frame));
});
it(@"pins to all edges", ^{
[subview pinToSuperviewEdges:JRTViewPinAllEdges inset:0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeTop, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeLeft, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeBottom, 0));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeRight, 0));
expect(superview.constraints).to.haveCountOf(4);
expect(CGRectGetMinY(subview.frame)).to.equal(0);
expect(CGRectGetMinX(subview.frame)).to.equal(CGRectGetMinX(superview.frame));
expect(CGRectGetMaxY(subview.frame)).to.equal(CGRectGetMaxY(superview.frame));
expect(CGRectGetMaxX(subview.frame)).to.equal(CGRectGetMaxX(superview.frame));
});
it(@"works with positive insets", ^{
CGFloat inset = 10;
[subview pinToSuperviewEdges:JRTViewPinTopEdge|JRTViewPinBottomEdge inset:inset];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeTop, inset));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeBottom, -inset));
expect(superview.constraints).to.haveCountOf(2);
CGRect expected = (CGRect) {
.origin.x = CGRectGetMinY(superview.frame),
.origin.y = inset,
.size.width = 0,
.size.height = CGRectGetHeight(superview.frame) - (inset * 2)
};
expect(subview).to.haveFrame(expected);
});
it(@"works with negative insets", ^{
CGFloat inset = -10;
[subview pinToSuperviewEdges:JRTViewPinTopEdge|JRTViewPinBottomEdge inset:inset];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeTop, inset));
expect(superview).to.haveConstraint(JRTEdgeConstraintWithEdgeAndEqualInset(NSLayoutAttributeBottom, -inset));
expect(superview.constraints).to.haveCountOf(2);
CGRect expected = (CGRect) {
.origin.x = CGRectGetMinY(superview.frame),
.origin.y = inset,
.size.width = 0,
.size.height = CGRectGetHeight(superview.frame) - (inset * 2)
};
expect(subview).to.haveFrame(expected);
});
it(@"uses a UIEdgeInsets value", ^{
UIEdgeInsets insets = (UIEdgeInsets){.top = 10.0,.right = 20.0,.bottom = 30.0,.left = 40.0};
[subview pinToSuperviewEdgesWithInset:insets];
[superview layoutIfNeeded];
expect(superview.constraints).to.haveCountOf(4);
CGRect expected = UIEdgeInsetsInsetRect(superview.bounds, insets);
expect(subview).to.haveFrame(expected);
});
});
context(@"When pinning to layout guides", ^{
it(@"uses the layout guides from the view controller",^{
UIViewController *vc = [UIViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
UIView *view = [UIView autoLayoutView];
[vc.view addSubview:view];
[view pinToSuperviewEdges:JRTViewPinTopEdge | JRTViewPinBottomEdge inset:0.0 usingLayoutGuidesFrom:vc];
[navController.view layoutIfNeeded];
XCTAssertNotNil(vc.topLayoutGuide);
XCTAssertNotNil(vc.bottomLayoutGuide);
expect(vc.view).to.haveConstraint([NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:vc.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]);
expect(vc.view).to.haveConstraint([NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:vc.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
});
});
context(@"when pinning attributes",^{
__block UIView *view1 = nil;
__block UIView *view2 = nil;
beforeEach(^{
superview = [UIView autoLayoutView];
view1 = [UIView autoLayoutView];
view2 = [UIView autoLayoutView];
[superview addSubview:view1];
[superview addSubview:view2];
});
it(@"pins one attribute to another",^{
[view1 pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeTop ofItem:view2];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
});
it(@"pins one attribute to another using a constant",^{
[view1 pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeTop ofItem:view2 withConstant:10.0];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0]);
});
it(@"pins one attribute to another using a constant and relation",^{
[view1 pinAttribute:NSLayoutAttributeTop toAttribute:NSLayoutAttributeTop ofItem:view2 withConstant:10.0 relation:NSLayoutRelationGreaterThanOrEqual];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0]);
});
it(@"pins the same attributes",^{
[view1 pinAttribute:NSLayoutAttributeTop toSameAttributeOfItem:view2];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
});
it(@"pins the same attributes with a constant",^{
[view1 pinAttribute:NSLayoutAttributeTop toSameAttributeOfItem:view2 withConstant:10.0];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0]);
});
});
context(@"when pinning edges of peer views",^{
__block UIView *view1 = nil;
__block UIView *view2 = nil;
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 300.0)];
view1 = [UIView autoLayoutView];
view2 = [UIView autoLayoutView];
[superview addSubview:view1];
[superview addSubview:view2];
[view1 constrainToSize:CGSizeMake(100.0, 100.0)];
[view1 centerInView:superview];
});
it (@"pins the top edge",^{
[view2 pinEdges:JRTViewPinTopEdge toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
XCTAssertEqual(100.0, CGRectGetMinY(view2.frame));
});
it (@"pins the left edge",^{
[view2 pinEdges:JRTViewPinLeftEdge toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]);
XCTAssertEqual(100.0, CGRectGetMinX(view2.frame));
});
it (@"pins the bottom edge",^{
[view2 pinEdges:JRTViewPinBottomEdge toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]);
XCTAssertEqual(200.0, CGRectGetMaxY(view2.frame));
});
it (@"pins the right edge",^{
[view2 pinEdges:JRTViewPinRightEdge toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]);
XCTAssertEqual(200.0, CGRectGetMaxX(view2.frame));
});
it (@"pins a pair of edges",^{
[view2 pinEdges:JRTViewPinTopEdge | JRTViewPinLeftEdge toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]);
XCTAssertEqual(100.0, CGRectGetMinY(view2.frame));
XCTAssertEqual(100.0, CGRectGetMinX(view2.frame));
});
it (@"pins all edges",^{
[view2 pinEdges:JRTViewPinAllEdges toSameEdgesOfView:view1];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]);
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]);
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]);
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]);
expect(view2).to.haveFrame(view1.frame);
});
it (@"pins the top edge with a positive inset",^{
[view2 pinEdges:JRTViewPinTopEdge toSameEdgesOfView:view1 inset:10.0];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0]);
XCTAssertEqual(110.0, CGRectGetMinY(view2.frame));
});
it (@"pins the top edge with a negative inset",^{
[view2 pinEdges:JRTViewPinTopEdge toSameEdgesOfView:view1 inset:-10.0f];
[superview layoutIfNeeded];
expect(superview).to.haveConstraint([NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:-10.0f]);
XCTAssertEqual(90.0, CGRectGetMinY(view2.frame));
});
});
context(@"when pinning specific points", ^{
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
subview = [UIView autoLayoutView];
[superview addSubview:subview];
[subview constrainToSize:CGSizeMake(100, 100)];
});
it(@"pins the left", ^{
[subview pinPointAtX:NSLayoutAttributeLeft Y:NSLayoutAttributeNotAnAttribute toPoint:CGPointMake(10.0,0.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMinX(subview.frame));
});
it(@"pins the center x", ^{
[subview pinPointAtX:NSLayoutAttributeCenterX Y:NSLayoutAttributeNotAnAttribute toPoint:CGPointMake(10.0,0.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMidX(subview.frame));
});
it(@"pins the right", ^{
[subview pinPointAtX:NSLayoutAttributeRight Y:NSLayoutAttributeNotAnAttribute toPoint:CGPointMake(10.0,0.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMaxX(subview.frame));
});
it(@"pins the top", ^{
[subview pinPointAtX:NSLayoutAttributeNotAnAttribute Y:NSLayoutAttributeTop toPoint:CGPointMake(0.0,10.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMinY(subview.frame));
});
it(@"pins the center y", ^{
[subview pinPointAtX:NSLayoutAttributeNotAnAttribute Y:NSLayoutAttributeCenterY toPoint:CGPointMake(0.0,10.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMidY(subview.frame));
});
it(@"pins the bottom", ^{
[subview pinPointAtX:NSLayoutAttributeNotAnAttribute Y:NSLayoutAttributeBottom toPoint:CGPointMake(0.0,10.0)];
[superview layoutIfNeeded];
XCTAssertEqual(10.0, CGRectGetMaxY(subview.frame));
});
it(@"pins the baseline",^{
UILabel *label = [UILabel autoLayoutView];
label.text = @"TEXT";
[superview addSubview:label];
[label centerInContainerOnAxis:NSLayoutAttributeCenterX];
[label pinPointAtX:NSLayoutAttributeNotAnAttribute Y:NSLayoutAttributeBaseline toPoint:CGPointMake(0.0,50.0)];
[superview layoutIfNeeded];
XCTAssertEqualWithAccuracy(50.0, CGRectGetMaxY(label.frame) + label.font.descender, 1.0);
});
});
context(@"when spacing views", ^{
__block UIView *view1 = nil;
__block UIView *view2 = nil;
__block UIView *view3 = nil;
__block UIView *view4 = nil;
__block NSArray *views = nil;
beforeEach(^{
superview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 410, 300)];
view1 = [UIView autoLayoutView];
view2 = [UIView autoLayoutView];
view3 = [UIView autoLayoutView];
view4 = [UIView autoLayoutView];
views = @[view1,view2,view3,view4];
[superview addSubview:view1];
[superview addSubview:view2];
[superview addSubview:view3];
[superview addSubview:view4];
});
it(@"spaces the views with a set spacing", ^{
[view1 pinToSuperviewEdges:JRTViewPinTopEdge inset:0.0];
[superview spaceViews:views onAxis:UILayoutConstraintAxisHorizontal withSpacing:10.0 alignmentOptions:NSLayoutFormatAlignAllTop];
[superview layoutIfNeeded];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
XCTAssertEqual(view.frame.size.width,90.0);
XCTAssertEqual(CGRectGetMinX(view.frame),10.0 + idx * 100);
}];
});
it(@"spaces the views at regular points", ^{
superview.frame = CGRectMake(0, 0, 400, 300);
[view1 pinToSuperviewEdges:JRTViewPinTopEdge inset:0.0];
[superview spaceViews:views onAxis:UILayoutConstraintAxisHorizontal];
[superview layoutIfNeeded];
CGFloat unitSize = CGRectGetWidth(superview.frame) / ([views count] + 1.0f);
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
XCTAssertEqual(CGRectGetMidX(view.frame),unitSize * (idx + 1));
}];
});
it(@"allows the first item to have flexible width",^{
superview.frame = CGRectMake(0,0,320,300);
views = @[view1,view2,view3];
[view1 pinToSuperviewEdges:JRTViewPinTopEdge inset:0.0];
[superview spaceViews:views onAxis:UILayoutConstraintAxisHorizontal withSpacing:0.0 alignmentOptions:NSLayoutFormatAlignAllTop flexibleFirstItem:YES];
XCTAssertEqual(CGRectGetWidth(view2.frame), CGRectGetWidth(view3.frame));
XCTAssertEqualWithAccuracy(CGRectGetWidth(view1.frame), CGRectGetWidth(view2.frame), 1.0f);
});
it(@"doesnt space the edges", ^{
[view1 pinToSuperviewEdges:JRTViewPinTopEdge inset:0.0];
[superview spaceViews:views onAxis:UILayoutConstraintAxisHorizontal withSpacing:10.0 alignmentOptions:NSLayoutFormatAlignAllTop flexibleFirstItem:NO applySpacingToEdges:NO];
[superview layoutIfNeeded];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
XCTAssertEqual(view.frame.size.width, 95.0);
XCTAssertEqual(CGRectGetMinX(view.frame),(idx * 95) + (10.0 * idx));
}];
});
});
});
SpecEnd