Using Grid, a powerful two-dimensional layout
In Chapter 3, Exploring Advanced Components, we learned about LazyHGrid
and LazyVGrid
. These two container views provide an efficient way of drawing views in a two-dimensional layout. They are very efficient for a large set of embedded views because they only draw the displayed or about-to-be-displayed views. This efficiency comes with the limitation of having to specify the layout in one of the two dimensions explicitly. For example, in a LazyVGrid
, we need to specify the layout for the columns. Once SwiftUI calculates the column width depending on the horizontal space available, the number of columns to be displayed gets fixed, and the embedded views get positioned in a grid with fixed columns and an unbounded number of rows.
But what about if we wanted a view to span two columns? What about if we have a small set of views that we want to display in a two-dimensional layout, but we want full control of the alignment and spacing...