'Spacing for cells in LazyVGrid are not the same on all sides
I have a LazyVGrid, and I am trying to make the spacing for all the cells in the grid to be equal. However, the cells keep touching the edges of the screen, and I am struggling to make it equal to the rest of the spacing for each cell. This is how it looks:
This is my code:
import SwiftUI
struct HomeView: View {
var body: some View {
NavigationView {
ScrollView {
LazyVGrid(columns: [GridItem](repeating: GridItem(.flexible()), count: 2)) {
ForEach(0..<6) { selection in
SelectionView()
}
}
}
.navigationTitle("The Deep End")
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct SelectionView: View {
var body: some View {
ZStack {
}
.frame(maxWidth: .infinity, minHeight: 100)
.background(Color.green)
.cornerRadius(15)
}
}
Solution 1:[1]
Here is a demo code specifying all available spacing options to 20.
ScrollView {
// VV horizontal spacing VV vertical spacing
LazyVGrid(columns: [GridItem](repeating: GridItem(.flexible(), spacing: 20), count: 2), spacing: 20) {
ForEach(0..<6) { selection in
SelectionView()
}
}
.padding(20) // << surrounding spacing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ChrisR |

