We call sizing behaviors 1 the various possibilities with which a view can decide the size it needs. Child views are positioned in their respective quadrant with different alignments (bottom left for the 1st, bottom right for the 2nd, top right for the 3rd and top left for the 4th), so that one of their corners coincides with the origin.ĭuring step 2 of the layout process children must decide the size they need before communicating it to their parent. A Plane might be made of two coordinate axes, equally offering a fourth of its size to four child views, each one drawn in a quadrant.A Border might offer its entire size to a single child view and adds a 1-pixel border on top of it.A Painting might draw some ostentatious painting frame around its edges, and offers the rest to a single child view centered in it.Size offers and child view placements vary depending on the expected result, for example: The parent lays out the child somewhere, strictly respecting the size that the child requested.It then returns the size it requires to its parent. The child view decides the size it requires, eventualy taking into account the parent size offer (a hint which the child is free to ignore entirely).The parent offers some size to the child view.When a parent must lay out one of its child views it proceeds in three steps, very well explained in articles from Alex Grebenyuk and Paul Hudson: Layouts themselves are simply created by assembling simple views and composed views in arbitrarily complex hierarchies. Composed views which take another view or a view builder as parameter, like VStack or Toggle.Simple views which do not take another view (or view builder) as parameter, like Text or Image. ![]() SwiftUI views all belong to either one of the following categories: UIViewRepresentable and UIViewControllerRepresentable.Determining the Intrinsic Sizing Behavior of a View.It finally provides a list of the sizing behaviors for most SwiftUI built-in views. It also introduces a formalism that helps characterize views and their sizing behaviors in general. This article explores the essential rules and behaviors of the SwiftUI layout system and explains how you should reason about it. Even for seasoned UIKit developers it can be difficult to figure out how SwiftUI layout system works, as its core principles are quite different from UIKit well-known concepts of Auto Layout constraints, springs and struts. The number of views and modifiers, as well as their various behaviors, can be quite overwhelming. But this does not mean how it works is entirely straightforward.įor newcomers with no preconception of how layout historically worked on Apple platforms, official documentation about the SwiftUI layout system might namely be incomplete or obscure. The SwiftUI layout system is more predictable and easier to understand than UIKit layout system. ![]() Understanding SwiftUI Layout Behaviors 3 June 2021
0 Comments
Leave a Reply. |