Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Auto Layout

Auto Layout

These slides are from a presentation on Auto Layout that I did for CocoaHeads Belgium in Nov. 2014, together with Tom Adriaenssen. You can find Tom's slides at https://speakerdeck.com/inferis/practical-autolayout.

Steven Vandeweghe

November 27, 2014
Tweet

More Decks by Steven Vandeweghe

Other Decks in Programming

Transcript

  1. Topics • The basics • Animation • Auto Layout in

    UIScrollView • Auto Layout in UITableViewCell • Examples
  2. Constraints • Describe relations between views using • Code •

    Visual Format Language • Interface Builder
  3. NSLayoutConstraint + (instancetype)constraintWithItem:(id)view1
 attribute:(NSLayoutAttribute)attr1
 relatedBy:(NSLayoutRelation)relation
 toItem:(id)view2
 attribute:(NSLayoutAttribute)attr2
 multiplier:(CGFloat)multiplier
 constant:(CGFloat)c -

    (void)addConstraint:(NSLayoutConstraint *)constraint Attributes: left, right, top, bottom, leading, trailing, width, height, centerX, centerY, baseline Relations: Equal, LessThanOrEqual, GreaterThanOrEqual
  4. Visual Format Language NSDictionary *views = NSDictionaryOfVariableBindings(view1, view2); NSArray *constraints

    = [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-5-[view1(100)]-10-[view2(>=200)]-5-|" options:0 metrics:0 views:views]; [self.view addConstraints:constraints];
  5. Content Hugging horizontal spacing = 20@1000 horizontal content hugging priority

    = 251 horizontal spacing = 20@500 horizontal content hugging priority = 251 horizontal spacing = 20@200 horizontal content hugging priority = 251
  6. Compression Resistance horizontal spacing = 20@1000 horizontal content hugging priority

    = 251 horizontal compression resistance = 750 left label compression resistance = 1000
  7. iOS 7 heightForRowAtIndexPath: dummyCell.titleLabel.text = @“text goes here”; [dummyCell.contentView setNeedsLayout];

    [dummyCell.contentView layoutIfNeeded]; height = [self.dummyCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; return ceil(height) + 1;
  8. iOS 7 MyCustomCell - (void)layoutSubviews { [super layoutSubviews]; [self.contentView layoutIfNeeded];

    self.titleLabel.preferredMaxLayoutWidth = self.titleLabel.frame.size.width; }
  9. iOS 8 heightForRowAtIndexPath: return UITableViewAutomaticDimension; If auto layout is detected:

    will call systemLayoutSizeFittingSize: If no auto layout is detected: will sizeThatFits: estimatedRowHeight needs to be known