🟠grid layout algorithm

⬆️ 需要: .dimension(), VGridForEach
struct GridLayoutView: View {
/// one .adaptive column
let oneAdaptive = [
GridItem(.adaptive(minimum: 50))
]
/// two .flexible columns
let twoFlexibles = [
GridItem(.flexible(minimum: 100)),
GridItem(.flexible(minimum: 180)),
]
/// three mixed columns
let threeMixed = [
GridItem(.flexible(minimum: 100)),
GridItem(.adaptive(minimum: 40)),
GridItem(.flexible(minimum: 180)),
]
/// view body
var body: some View {
VStack(spacing: 50) {
grid(items: 20, width: 400, columns: oneAdaptive, color: .purple)
grid(items: 4, width: 300, columns: twoFlexibles, color: .pink)
grid(items: 10, width: 300, columns: threeMixed, leading: true)
}
}
/// grid()
@ViewBuilder func grid(
items count: Int,
width : CGFloat, // proposed width
columns : [GridItem],
leading : Bool = false, // height dimension on leading side
color : Color = pacificBlue // item color
) -> some View {
VGridForEach(0..<count, columns: columns){ i in
color.frame(height: 50).dimension(.vcenter, arrow: darkCerulean)
}.framedDimension(width: width, border: .yellow, leading: leading)
}
}
// helper extension
extension View {
@ViewBuilder func framedDimension(
width : CGFloat, // view width
border color: Color = .yellow, // view's border color
leading : Bool = false // height dimensions on leading side
) -> some View {
self
.frame(width: width)
.border(color)
// 🌀 view.dimension()
.dimension(leading ? .bottomLeading : .bottomTrailing)
}
}
Last updated
Was this helpful?