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

iOS Auto Layout in practice

Avatar for Yi Gu Yi Gu
December 15, 2012

iOS Auto Layout in practice

Some tips about Auto Layout on iOS

Avatar for Yi Gu

Yi Gu

December 15, 2012
Tweet

Other Decks in Programming

Transcript

  1. • 2D rect • 4 parameters View Geometry • dimension

    (width, height) • position (origin.x, origin.y) Saturday, December 15, 12
  2. Constraints • Help the layout manager to calculate the geometry

    params during runtime • Relation between 2 views • Linear formula foo.left = 2 × bar.width + 100 • IB will generate adequate constraints automatically • For coding: visual format language & API Saturday, December 15, 12
  3. • To create user constraints • Add new constraints •

    Promote an automatic constraint Xcode & Constraints Saturday, December 15, 12
  4. • User constraints • Blue icon • Can be removed

    • Treat them as warnings Xcode & Constraints Saturday, December 15, 12
  5. • Constraint properties • Relation ( ==, <=, >= )

    • Constant • Priority (1~1,000) • No multiplier Xcode & Constraints Saturday, December 15, 12
  6. Rule of 4 To make a view fixed as expected,

    4 constraints are needed (2 constraints in each direction) no less than 4, no more than 4 Saturday, December 15, 12
  7. Rule of 4 To make a view fixed as expected,

    4 constraints are needed (2 constraints in each direction) no less than 4, no more than 4 …in most cases Saturday, December 15, 12
  8. Patterns of layout child-parent views Elastic layout • container •

    large content(image, text) • ... Classic layout • small controls • icons • ... Saturday, December 15, 12
  9. Patterns of layout multiple views Chained layout • a group

    of buttons • ... Parent-based layout views stay independently, may overlap ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ Saturday, December 15, 12
  10. Patterns of layout multiple views Chained layout • a group

    of buttons • ... Parent-based layout views stay independently, may overlap ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ ᶃ ᶄ ᶅ ᶆ ᶇ ᶈ Leave one dimension undefined as the tolerance to absorb the calculation errors Saturday, December 15, 12
  11. Layout trouble shooting • Ambiguous constraints • under-constrained (constraints <

    4) • rare case by using IB • Solutions • Try adding more constraints Saturday, December 15, 12
  12. Layout trouble shooting • Unsatisfied layout • over-constrained ( constraints

    > 4) • some views may end up with negative dimension • Solutions • Remove the conflicting constraints • Adjust the priority of constraints Saturday, December 15, 12
  13. Avoid using <= & => • Why • iOS has

    a fixed window size • Inequality constraints break the Rule of 4 • Redundant constraints • Hard to debug • Try using intrinsic content size & priority before adding inequality constraints Saturday, December 15, 12
  14. References • Cocoa Auto Layout Guide • iOS 6 Release

    Notes • A special case of UIScrollView • WWDC 2012 • Introduction to Auto Layout for iOS and OS X • Auto Layout by Example • Best Practices for Mastering Auto Layout Saturday, December 15, 12