I am trying to create a grid in SwiftUI 2 using the new primitives LazyVGrid/LazyHGrid.
My goal is to create a grid with the following properties:
- Each cell has the same width as height (basically a square)
- The grid has an equal number of rows and columns (Columns = Rows)
- The content should not be scrollable and always fit the size of my view/screen.
Here's a video with the expected results. This was achieved by a custom Grid object that uses GeometryReader.
I've tried using .contentMode(1, .fit) to restrain the LazyVGrid to a 1:1 ratio but it doesn't seem to work. The red background represents the frame of the Grid which is not the expected behaviour.
LazyVGrid(columns: [GridItem(.adaptive(minimum: 32), spacing: 0)], spacing: 0) {
ForEach(1...100, id: \.self) { item in
Rectangle()
.aspectRatio(1, contentMode: .fit)
.border(Color.black)
}
}
.aspectRatio(1, contentMode: .fit)
.background(Color.red)
I have also tried using GeometryReader to fix the size of the LazyVGrid, but it still didn't work. Any other ideas on how to achieve this?


