AtelierClockwork

Auto Layout from Scratch

September 18, 2015

Stacks, Anchors, Etc

Now that iOS 9 is out, it means I sat down and ported the layout code in my latest demo app from SnapKit to native auto layout.

At a high level, NSLayoutAnchor provides most of the power I wanted from an Auto Layout DSL, it doesn’t hit every case I wanted, particularly it doesn’t allow for creating constraints with multipliers other than 1.0, but it hit most of my use cases with minimal effort, and the ability to add arbitrary anchors to a view has potential to make implementing complex layouts much simpler in the future.

UIStackView let me tear out over half of my layout code completely, and the resulting code is much easier to understand. I have a properly laid out grid that uses nested stacks views and content hugging priority applied to subviews to ensure everything sizes correctly.

All in all, it was an easy transition from SnapKit, and I’m looking forward to really putting the new layout tools through their paces with more complex layouts.