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

Throwing Auto Layout out the window

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Throwing Auto Layout out the window

Auto Layout is Apple's recommended technology for building UIs on their platforms. We use a lot of Auto Layout at LinkedIn, but after we ran into some frustrating performance problems, we decided to build and open source our own solution called LayoutKit (http://layoutkit.org). Not only is LayoutKit much faster than Auto Layout, but it is also easier to use because layouts are declarative, immutable, and thread-safe.

Avatar for Nick Snyder

Nick Snyder

October 26, 2016
Tweet

Other Decks in Programming

Transcript

  1. Storytime about Auto Layout • How we use it at

    LinkedIn. • Problems we have encountered. • Why we stopped using it in certain places. • What we are using instead of Auto Layout.
  2. The good news • We use a lot of Auto

    Layout at LinkedIn. • It is powerful and makes it easy to support ◦ Multiple screen sizes ◦ Right to left languages
  3. The bad news Auto Layout performance isn’t good enough. •

    Doesn’t scale to complicated view hierarchies. • Performance regressions in iOS releases. • Unpredictably performance for some layouts.
  4. Auto Layout performance regressions ✓ iOS 8 X iOS 9

    Label w/barely two lines Right label Required content hugging Required content compression resistance
  5. LinkedIn news feed • Hasn’t used Auto Layout for a

    while (because performance). • Each view implements manual layout code. ◦ Tedious to maintain ◦ Wanted a reusable solution to share with Profile.
  6. Requirements for layout solution • Fast (on par with writing

    layout code manually). • API that is natural to use in a Swift app. • Maintained and have non-trivial adoption. • Open source (debug when things go wrong). • Acceptable license.
  7. Available solutions • React Native, AsyncDisplayKit, ComponentKit ◦ Licensing •

    Few (Last commit over one year ago) • Render (Did not exist. Created May 2016) None of these projects satisfied all of our requirements.
  8. LayoutKit A fast view layout library for iOS, macOS, and

    tvOS. • How do you use it? • How does it work?
  9. High level flow 1. Developer defines a layout using immutable

    data structures. 2. LayoutKit computes view frames (on a background thread if you want). 3. LayoutKit creates views and assigns frames (on the main thread).
  10. Declaring a layout let image = SizeLayout<UIImageView>( width: 50, height:

    50, config: { imageView in imageView.image = UIImage(named: “earth.jpg”) } )
  11. Declaring a layout let helloWorld = InsetLayout( insets: UIEdgeInsets(top: 4,

    left: 4, bottom: 4, right: 8), sublayout: stack )
  12. Using a layout // May be done on a background

    thread. let arrangement = helloWorld.arrangement() // Must be done on the main thread. arrangement.makeViews(in: rootView)
  13. Animating a layout // Give animated layouts a viewReuseId let

    before = SizeLayout( width: 50, height: 50, viewReuseId: “box”) let after = SizeLayout( width: 25, height: 25, viewReuseId: “box”)
  14. Animating a layout // Initial layout. before.arrangement().makeViews(in: rootView) // Prepare

    the animation. let animation = after.arrangement() .prepareAnimation(for: rootView)
  15. Swift LayoutKit takes advantage of Swift features to provide a

    clean API. • Generics • Protocol extensions • Initializer parameters with default values
  16. Basic layouts Just math packaged in the Layout protocol •

    LabelLayout (124 LOC) • SizeLayout (164 LOC) • InsetLayout (39 LOC) • StackLayout (175 LOC) Layouts
  17. Custom layouts • Can create a lot of useful layouts

    by composing and nesting the four basic layouts. • Can create custom layouts by implementing the Layout protocol. Layout Protocol
  18. Why is LayoutKit fast? • Not a generic constraint solver.

    ◦ Specialized algorithm per layout. • Slowest algorithmic complexity is sorting sublayouts by flexibility in StackLayout. ◦ Everything else is O(n). Layout Engine
  19. Immutable data structures Layout objects and all intermediate data structures

    are immutable. • Thread safety. • Easy to cache and/or precompute. • Easy to debug. Layout Engine
  20. Other benefits • Handles right-to-left languages. • Declarative layouts are

    easy to read, write, compose, and test. • Can prototype layouts quickly in playgrounds. • Supports iOS, macOS, and tvOS. Layout Engine
  21. Adoption • Used on certain screens in the main LinkedIn

    app as well as the Job Search app. • Onboarding engineers has been easy.
  22. Open source! • http://layoutkit.org • Apache 2.0 license ◦ No

    patent shenanigans • Released June 22, 2016 • Today: