ๆฌ็ฏไพ้็ถๅชๆๆๅฎๅ
ฉๅ GridItem ๏ผ
Copy let columns = [
GridItem ( .fixed ( 70 )) , // โญ fixed column
GridItem ( .adaptive ( minimum : 40 )) // โญ adaptive column
]
ไฝ็ตๆๅปๅบ็พไธๅๆฌไฝ ๏ผ้ไธป่ฆๆฏๅ ็บๅ
ถไธญไธๅๆฏ adaptive column ๏ผ้็จฎๆฌไฝๅฏไปฅไธๆฌกๅฎน็ดๅคๅ้
็ฎ (cells )ใ
Copy // ------------------------
// โญ Fixed Column
// ------------------------
//
// always rendered at the specified width
// (no matter how much space is available)
// ---------------------------
// โญ Adaptive Column
// ---------------------------
//
// โญ tries to accommodate "as many items as possible"
// โญ the "minimum" property of an "adaptive column" is only used
// to compute how many items that column can accommodate,
// it's NOT the minimum width of that columnโ๏ธ
// (adaptive column's width could be SMALLER than this property)
// โญ 1. (remaining width)
// = (grid width) - (fixed widths) - (spaces between 2 columns)
// = (200) - (70) - (8 * 1)
// = (122)
//
// โญ 2. calculate (how many items) an adaptive column can accommodate:
// (remaining width) - (first item) - (following items) > 0
// given:
// minimum width = (40), spacing = (8) : โญ default
// fist item = (40), following item = (8 + 40)
//
// (122) - (40) - (48) * 1 = 34 > 0 ... โ
(accommodate 2 items)
// (122) - (40) - (48) * 2 = -14 < 0 ... โ
//
// โญ 3. (item width)
// = [(remaining) - (spaces between 2 items)] / (2 items)
// = [(122) - (8 * 1)] / 2
// = 114 / 2
// = 57 ............ (proposed size for each item) โญ
//
// โญ result:
//
// โญโโfixedโโโฎ โญโโโโโ adaptive โโโโโโโฎ (one adaptive column)
// [ 70 ][8][ 57 ][8][ 57 ]
// โฐโโitemโโโฏ โฐโโitemโโโฏ (two grid items)
struct ContentView: View {
let columns = [
GridItem(.fixed(70)), // โญ fixed column
GridItem(.adaptive(minimum: 40)) // โญ adaptive column
]
var body: some View {
grid(columns)
}
}
extension ContentView {
/// a grid of swatches
func grid(_ columns: [GridItem]) -> some View {
// ๐ ScrollVGridForEach
ScrollVGridForEach(0..<100, columns: columns){
numberSwatch($0)
}
.frame(width: 200) // grid width = scroll view width โญ
.border(Color.yellow) // scroll view border (yellow)
.padding()
.border(Color.blue) // padding border (blue)
.frame(height: 300)
}
/// a color swatch with a number on it
@ViewBuilder func numberSwatch(_ i: Int) -> some View {
let color: Color = i % 3 == 0 ? .green : .pink
color
.opacity(0.3)
.frame(height: 40)
.overlay(Text("\(i)"))
}
}