网格布局
1. 使用 LazyVGrid 实现垂直网格
LazyVGrid 实现垂直网格struct GridExample: View {
let columns = [
GridItem(.fixed(100)), // 固定列宽
GridItem(.flexible()), // 灵活列宽
GridItem(.adaptive(minimum: 50)) // 自适应列宽
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns, spacing: 20) {
ForEach(0..<50) { index in
Text("Item \(index)")
.frame(height: 100)
.background(Color.blue)
.cornerRadius(8)
}
}
.padding()
}
}
}2. 使用 LazyHGrid 实现水平网格
LazyHGrid 实现水平网格3. 自适应网格 (.adaptive)
.adaptive)4. 自定义网格项间距
总结
Last updated