Member-only story
The SwiftUI Layout Protocol in iOS 16
A practical look at the new protocol introduced in SwiftUI
When I started coding, there were no frameworks to speak of. When you wanted to plot a circle on the screen, it took dozens or more lines of code than today. As science has developed, the concept of abstraction and frameworks has become the way forward.
Today, if I want to draw a circle using the SwiftUI framework, it takes just a couple of lines. It is good, but there is a price to pay; the higher the abstraction, the less flexibility you have, a trade-off.
When Apple launched SwiftUI, one of the goals was to make the layout of screens far easier than it has been for the past two decades with UIKit. It introduces three primary containers to lay out your windows: HStack, VStack, and ZStack. But for some, it was too high a price to do the layouts they had gotten used to in UIKit. They looked complex and messy in SwiftUI.
Apple has tried to address this by introducing the layout protocol, exposing the underbelly of those principle containers so that you can change how they behave. Join me in this article to look at some practical uses and implementations of this new protocol to let us create our containers with their own layout rules.