๐ฆGridItem
Last updated
Last updated
SwiftUI โฉ Layout โฉ LazyGrids โฉ
adaptive column - ่ชชๆ adaptive column ๅฆไฝ่จ็ฎๆฌไฝๆธ้่ๅฏฌๅบฆใ
The spacing value of the last GridItem is ignored.
The height of a row in a LazyVGrid is driven by the height of the tallest cell.
struct ContentView: View {
// 3 columns
let threeColumns = [
// first col
GridItem( // controls: size, spacing, alignment
.fixed(60), // โญ๏ธ column width (enum GridItem.Size)
spacing : 10, // โญ๏ธ space to next COLUMN
alignment: .topTrailing // โญ๏ธ alignment in a CELL
),
// second col
GridItem(.fixed(120), spacing: 30, alignment: .center),
// โญ๏ธ last column's `spacing` is ignored
GridItem(.fixed(100), spacing: 10, alignment: .bottomTrailing),
]
var body: some View {
// vertical scroll view (by default)
ScrollView {
// โญ๏ธ VERTICAL columns
// - cells flow HORIZONTALLY
LazyVGrid(
columns: threeColumns, // โญ๏ธ columns setting
alignment: .center, // โญ๏ธ for the cells as a whole
spacing: 10 // โญ๏ธ space between ROWS
) {
ForEach(0..<100) { i in
VStack {
switch i {
case 3: rect(.pink)
case 4: rect(.green).frame(height: 50)
case 5: rect(.blue).frame(width: 60, height: 30)
case 6: rect(.yellow).frame(width: 80)
default: rect(.gray4).frame(height: 30)
}
}.overlay(Text("\(i)"))
}
}
.border(Color.orange) // grid border
.frame(width: 500)
.padding(20)
}
.frame(height: 200)
.border(Color.blue) hi// scroll view border
}
}
extension ContentView {
func rect(_ color: Color) -> some View {
Rectangle().fill(color)
}
}
โฌ๏ธ ้่ฆ๏ผ Repeatable
// โญ๏ธ GridItem + ๐
ฟ๏ธ Repeatable
extension GridItem: Repeatable {}
let columns = GridItem(.flexible()) * 2
Swift โฉ Array โฉ .init(repeating:count:)
SwiftUI โฉ View Layout and Presentation โฉ
ๅ๏ผใ็ด ่ฒใ่ใ้ป่ฒใ่ฒๅก frame ๆฒๆๆๅฎ height ๅป็ธฎๅฐๆๅฐ้ซๅบฆโ