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

Hands On with Adaptive Layout

sammyd
March 27, 2015

Hands On with Adaptive Layout

Adaptive Layout is an important part of iOS 8, leveraging Auto Layout to adapt the same design to the plethora of form-factors available in the iOS world. This talk runs through the basics of working with adaptive layout, although the most important part is a demo, which is not in the slides. Good luck.

sammyd

March 27, 2015
Tweet

More Decks by sammyd

Other Decks in Programming

Transcript

  1. in the past if UIDevice.currentDevice().userInterfaceIdiom == .Pad { if UIDevice.currentDevice().orientation

    == .LandscapeLeft || UIDevice.currentDevice().orientation == .LandscapeRight { doSomething() } else { doSomethingElse() } } else { if UIDevice.currentDevice().orientation == .LandscapeLeft || UIDevice.currentDevice().orientation == .LandscapeRight { yetAnotherAlternative() } else { theFourthWay() } }
  2. adaptive layout • Abstracts layout away from device specifics •

    Introduces size classes • Available in iOS 8 • Fully supported in Xcode and IB
  3. today • Intro to Size Classes • Demo of installable

    constraints & views in IB • Adaptive fonts and images • Adaptive Layout in code
  4. size classes Horizontal Vertical iPad Portrait Regular Regular iPad Landscape

    Regular Regular iPhone Portrait Compact Regular iPhone Landscape Compact Compact iPhone 6 Plus Landscape Regular Compact
  5. approach 1. Build base layout 2. Choose size class override

    3. Uninstall irrelevant constraints 4. Add new constraints specific to size class 5. Rinse and repeat
  6. UITraitCollection class UITraitCollection : NSObject,... { var userInterfaceIdiom: UIUserInterfaceIdiom {

    get } var displayScale: CGFloat { get } var horizontalSizeClass: UIUserInterfaceSizeClass { get } var verticalSizeClass: UIUserInterfaceSizeClass { get } ... }
  7. UITraitEnvironment /*! Trait environments expose a trait collection that describes

    their environment. */ protocol UITraitEnvironment : NSObjectProtocol { var traitCollection: UITraitCollection { get } ... }
  8. UITraitEnvironment /*! Trait environments expose a trait collection that describes

    their environment. */ protocol UITraitEnvironment : NSObjectProtocol { var traitCollection: UITraitCollection { get } ... } UIScreen UIWindow UIPresentationController UIView UIViewController
  9. handling change protocol UITraitEnvironment : NSObjectProtocol { ... /*! To

    be overridden as needed to provide custom behavior when the environment's traits change. */ func traitCollectionDidChange( previousTraitCollection: UITraitCollection?) } UIScreen UIWindow UIPresentationController UIView UIViewController
  10. handling change protocol UIContentContainer : NSObjectProtocol { func viewWillTransitionToSize(size: CGSize,

    withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) } UIScreen UIWindow UIPresentationController UIView UIViewController
  11. where next? • Adaptive View Controller Hierarchies • Adaptive Presentation

    • WWDC Videos • Ray Wenderlich blog posts / book
  12. takeaways • Universal storyboards FTW • Can go far without

    code • Size classes conceptually equivalent to subclassing