Slide 1

Slide 1 text

AUTO LAYOUT BOOTCAMP CocoaShops Curtis Herbert Amit Rao Kotaro Fujita @parrots @amitmrao @wild37

Slide 2

Slide 2 text

IN THE BEGINNING Frames, Springs & Struts

Slide 3

Slide 3 text

0,0 320,480 70, 150 200 250 320, 0 0, 480

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

[view1 setFrame:CGRectMake(0, 150, 100, 100)]; [view2 setFrame:CGRectMake(0, 160, 100, 100)];

Slide 8

Slide 8 text

[view1 setFrame:CGRectMake(0, 150, 100, 100)]; [view2 setFrame:CGRectMake(0, CGRectGetMaxY(view1.frame) + 10, 100, 100)];

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

2011 2012

Slide 11

Slide 11 text

2013 ( ) Xcode 5

Slide 12

Slide 12 text

WHAT IS AUTO LAYOUT? • Series of rules, or constraints, you set up • Based on the rules system lays out interface, responds to changes • Rules must yield a valid state, can’t be ambiguous

Slide 13

Slide 13 text

WHY AUTO LAYOUT? Declarative Relational Expressive

Slide 14

Slide 14 text

WHAT MAKES A VALID STATE? X and Y for point of origin width and height

Slide 15

Slide 15 text

USING AUTO LAYOUT IN INTERFACE BUILDER

Slide 16

Slide 16 text

CREATING CONSTRAINTS Demo / Follow along

Slide 17

Slide 17 text

CREATING CONSTRAINTS Control-drag from view-to-view • within the scene • on the left

Slide 18

Slide 18 text

CREATING CONSTRAINTS Editor menu w/ view selected

Slide 19

Slide 19 text

CREATING CONSTRAINTS Bottom right tools

Slide 20

Slide 20 text

EDITING CONSTRAINTS IN IB Demo / Follow along

Slide 21

Slide 21 text

EDITING CONSTRAINTS Edit on the view

Slide 22

Slide 22 text

EDITING CONSTRAINTS In the hierarchy

Slide 23

Slide 23 text

WHEN IB HATES YOU Demo / Follow along

Slide 24

Slide 24 text

FIXING AUTO LAYOUT COMPLAINTS

Slide 25

Slide 25 text

FIXING AUTO LAYOUT COMPLAINTS Bottom right tools

Slide 26

Slide 26 text

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "", "" ) Will attempt to recover by breaking constraint BE AWARE: RUNTIME CONSOLE ERRORS

Slide 27

Slide 27 text

PROTIP Very rarely do you want to “Add missing constraints” Don’t let the system do magic for you, know thy constraints.

Slide 28

Slide 28 text

PROTIP Watch out for those decimal constants.

Slide 29

Slide 29 text

PROTIP Use “Update Frames” on all views only when all views have constraints.

Slide 30

Slide 30 text

DIGGING IN DEEPER

Slide 31

Slide 31 text

CONSTRAINT TYPES Demo / Follow along

Slide 32

Slide 32 text

CONSTRAINT PROPERTIES Demo / Follow along

Slide 33

Slide 33 text

PUZZLE BREAK

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

INTRINSIC CONTENT SIZE Demo / Follow along

Slide 37

Slide 37 text

- (CGSize)intrinsicContentSize { ... } IN-CODE

Slide 38

Slide 38 text

TEMPORARY SIZING

Slide 39

Slide 39 text

TEMPORARY SIZING

Slide 40

Slide 40 text

LETS GET DANGEROUS

Slide 41

Slide 41 text

SCROLLVIEWS () Demo / Follow along

Slide 42

Slide 42 text

JUST REMEMBER Need to be able to connect the dots, so to speak, to get a defined content block from top-to-bottom and left-to-right

Slide 43

Slide 43 text

CONSTRAINT PRIORITIES Demo / Follow along

Slide 44

Slide 44 text

UILayoutPriorityRequired = 1000, // Required UILayoutPriorityDefaultHigh = 750, // Compression resistance UILayoutPriorityDefaultLow = 250, // Compression hugging UILayoutPriorityFittingSizeLevel = 50, // System layout fitting size SOME DEFAULTS

Slide 45

Slide 45 text

COMPRESSION / HUGGING Demo / Follow along

Slide 46

Slide 46 text

Hugging: higher = don’t want to grow. Compression: higher = fights clipping/shrinking. JUST REMEMBER

Slide 47

Slide 47 text

JUST REMEMBER Only affects items with an intrinsic content size. Won’t affect items with constraints for width and height.

Slide 48

Slide 48 text

ANIMATING CONSTRAINTS Demo / Follow along

Slide 49

Slide 49 text

self.myConstraint.constant = 15.0; [containerView setNeedsUpdateConstraints]; //key step 1 [UIView animateWithDuration:0.25f animations:^{ [containerView layoutIfNeeded]; //key step 2 }]; ANIMATING CONSTRAINTS

Slide 50

Slide 50 text

PROTIP Animating and want to affect entire screen? IBOutlet Animating and don’t want to affect anything else? Core Animation Transforms

Slide 51

Slide 51 text

CONSTRAINTS IN CODE Demo / Follow along

Slide 52

Slide 52 text

[NSLayoutConstraint constraintWithItem:viewA attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:viewB attribute:NSLayoutAttributeWidth multiplier:0.0 constant:0.0]; CONSTRAINTWITHITEM

Slide 53

Slide 53 text

UIView *superview = self; UIView *view1 = [[UIView alloc] init]; view1.translatesAutoresizingMaskIntoConstraints = NO; view1.backgroundColor = [UIColor greenColor]; [superview addSubview:view1]; UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:padding.top], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:padding.left], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1 constant:-padding.right], ]]; MESSY FAST

Slide 54

Slide 54 text

NSDictionary *views = NSDictionaryOfVariableBindings(one, two, three); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@“H:|-[one(two)]-[two(three)]- [three]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:views]]; VISUAL FORMAT LANGUAGE

Slide 55

Slide 55 text

USE A COCOA POD! [view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding); }]; https://github.com/Masonry/Masonry

Slide 56

Slide 56 text

USE A COCOA POD! view1.snp_makeConstraints { make in make.edges.equalTo(superview).with.insets(padding) } https://github.com/Masonry/Snap

Slide 57

Slide 57 text

OVERRIDING AUTOLAYOUT Demo / Follow along

Slide 58

Slide 58 text

JUST REMEMBER layoutSubviews: UIView subclass viewDidLayoutSubviews: UIViewController subclass

Slide 59

Slide 59 text

PROTIP This technique should be used sparingly

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

TAKEAWAYS Define intent, don’t flail

Slide 62

Slide 62 text

TAKEAWAYS Less constraints the better

Slide 63

Slide 63 text

RESOURCES http://revealapp.com