Auto Layout

  • Auto Layout Menus (at bottom right of IB)
    1. Update Frames button (fixes misplaced views)
    2. Align menu
    3. Add New Constraints menu
    4. Resolve Auto Layout Issues menu
    5. Embed In menu
  • Auto Layout issues outline
    • Arrow in red circle appears next to scene icon in outline

Implicit Constraints

  • Content hugging
    • Makes the view not want to be bigger than its intrinsic content size in a given dimension
    • Think of it like a rubber band; higher value is stronger rubber band
    • 0: view is very willing to get bigger than its intrinsic content size in that dimension
    • 1000: view cannot get bigger than its intrinsic content size in that dimension
    • Example: if a view gets wider and there are two subviews, which of them gets bigger to fill it?
  • Content compression resistance
    • Makes the view not want to get smaller than its intrinsic content size in a given dimension
    • Example: if a view gets narrower and there are two subviews, which of them gets smaller to fit it?

Programmatically Creating Auto Layout Constraints

  • translatesAutoresizingMaskIntoConstraints = false
    • Autoresizing masks predate Auto Layout
    • By default, iOS will take the autoresizing mask and generate autolayout constraints. YOU DO NOT WANT THIS. Turn that off, then create the constraints yourself instead.
let margins = view.layoutMarginsGuide
let topConstraint = segmentedControl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
let leadingConstraint = segmentedControl.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
let trailingConstraint = segmentedControl.trailingAnchor.constraint(equalTo: margins.trailingAnchor)

// setting isActive = true triggers the constraint to add itself to the correct shared parent view
topConstraint.isActive = true
leadingConstraint.isActive = true
trailingConstraint.isActive = true

Size Classes

  • Compact Width, Compact Height - Smaller iPhones in landscape orientation
  • Compact Width, Regular Height - iPhones of all sizes in portrait orientation. Often apps running on iPad in Split View or Slide Over
  • Regular Width, Compact Height - Larger iPhones in landscape orientation
  • Regular Width, Regular Height - iPads of all sizes in all orientations