Announcing Modern Auto LayoutOct 23, 2018 · 6 minute read
Is learning Auto Layout making your head spin? About to throw your Mac out the window in frustration? Maybe I can help. It’s been a while in the making but today’s the day I launch my new book on Modern Auto Layout.
Just want the book? Go straight to the checkout.
Why Is Auto Layout So Hard?
When I ask iOS developers what part of their job they struggle with one of the top answers is Auto Layout. What makes it so hard? Let’s make a list…
It’s hard to know when you’re done. How many constraints do you need? Where should you create your constraints?
Horizontal and vertical content-hugging and compression-resistance priorities. Need I say more? Every view has four priorities. Flexibility is great, but it’s hard to imagine a more complicated mechanism for controlling what gets stretched and squeezed.
You don’t have a good mental model for what the layout engine is doing. Auto Layout feels like a closed magic box. You feed it some constraints, and a layout pops out that may or, more often, may not be what you wanted.
Heard that you should start with stack views? They help but do a bad job of hiding complexity. You still need some constraints and there’s no escaping the twin horrors of content-hugging and compression resistance priorities. I have ranted about some stack view oddities and bugs before. Stack views are a huge time saver, but I wouldn’t learn them first.
Auto Layout is a descriptive layout system, but it’s a low-level description. You still need to do most of the work to make modern adaptive layouts.
I could go on…
Layout Is More Than Auto Layout
It’s not enough to know Auto Layout these days. Modern iOS apps need to adapt to a wide range of situations:
Layouts need to scale from the smallest iPhone SE up to the largest 12.9” iPad Pro and work in slide over and split screen modes.
Each year brings more devices and more user interface variations. The iPhone X brought us the top sensor housing (the “notch”) and home screen indicator. This year we have already seen new screen sizes with the iPhone XR, and iPhone XS Max and new iPads are on the way. You need to be able to support these variations quickly.
Your users increasingly expect your App to support features like dynamic type. PSPDFKit recently shared that around 27% of users of their PDF viewer use non-default text sizes.
Dynamic sizing needs a dynamic layout. That means building for dynamic type, readable content, self-sizing table view cells, size classes and more. You don’t have to use Auto Layout, but many of the above challenges become manageable when you do.
What Is Modern Auto Layout?
What do I mean by Modern Auto Layout? I think of it as the version of Auto Layout that Apple introduced in iOS 9 and has refined and improved in each release since then:
In iOS 9, Apple added layout anchors and layout guides. They also added stack views and using Auto Layout got a whole lot less painful.
In iOS 10, adopting dynamic type became less work with automatic font adjustments to content size changes.
In iOS 11, safe area layout guides and margins that know about safe areas replaced top and bottom layout guides. Font metrics make it easier to use custom fonts with dynamic type. Scroll views got layout guides, and stack views got custom spacing.
In iOS 12, Apple improved the performance of Auto Layout.
What’s In The Book?
The first part of the book covers the fundamentals of Auto Layout. The key concepts that make it work and the tools to apply it to your layouts:
Layout before Auto Layout: We start by looking at how we did layout before Auto Layout, manually managing the frames of views and relying on autoresizing or when that’s insufficient, by overriding
layoutSubviews. We also look at how to create an Xcode project to work without storyboards.
Getting Started With Auto Layout: Introduces you to constraints, what they are, who owns them and how many you need to create common layouts.
Using Interface Builder: We dive into using Interface Builder. We look at the many ways to create constraints and what the warnings and errors mean. We also have lots of useful tips and tricks to help you master Interface Builder.
Creating Constraints In Code: You don’t have to use Interface Builder to use Auto Layout. Learn the three ways Apple gives us to create constraints in code and why you want to use layout anchors.
Safe Areas And Layout Margins: Safe Area Layout Guides became a hot topic when Apple launched the iPhone X. Learn how to keep the system from clipping or covering your content and how to fall back to the top and bottom layout guides for iOS 9 and 10. Learn how to use layout margins and layout guides.
Layout Priorities And Content Size: We cover the topics that cause many people to hate Auto Layout. The tricky concepts of layout priorities, intrinsic content size, and content-hugging and compression-resistance.
Stack Views: A welcome and overdue addition in iOS 9. Build layouts without having to manually create every constraint in Interface Builder or with pages of boilerplate code. We also cover some useful improvements that came in iOS 11 and some oddities to avoid.
Understanding The Layout Engine: Time to dig deep into how the layout engine works to translate your constraints into a working layout. Why you probably shouldn’t be using
updateConstraints, how to animate changes and take full control to build custom layouts.
Debugging When It Goes Wrong: How do you debug your layouts when they go wrong? We look at the tools and techniques to understand and fix your Auto Layout problems.
With the foundation built the second part of the book looks at how to use Auto Layout with related API’s to build adaptive layouts.
Scroll Views And Auto Layout: The scroll view is an essential view to master. We use it frequently in later chapters to build more adaptive layouts. It improved in iOS 11, but can still be confusing to use with Auto Layout.
Dynamic Type: Dynamic type puts the user in control of the size of text in your App. Learn how to use dynamic type, adapt your layouts to cope with dramatic changes in text size and use custom fonts with dynamic type.
Working With Table Views: Self-sizing table view cells are a regular source of pain and confusion. Learn how they work and how to use readable content guides with table views.
Adapting For Size: We bring everything together to build layouts that adapt to the size of the screen. Learn how to use trait collections and size classes, create asset variations and variable width strings. Finally, we go beyond size classes to build adaptive layouts based on the available space.
Buy Modern Auto Layout
- Multiple formats: PDF (480 pages), ePUB (iBooks) and MOBI (Kindle)
- DRM free. Read on all of your devices that support PDF, ePUB or MOBI formats, without restriction.
- Updated for Xcode 11, Swift 5 and iOS 13.
- 80 sample Xcode projects available for download from the book's source code repository.
- 32 layout challenges to practice your layout skills as you learn. Full solutions in the book's source code repository.