我目前有这样的
LazyVGrid
设置:
struct NetworkGrid: View {
var networks: [Network]
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
var body: some View {
ScrollView {
LazyVGrid(columns: columns) {
ForEach(networks) { network in
NetworkCard(network: network)
}
}
}
}
}
我想根据当前窗口大小设置网格列数,即
func windowDidResize(_ notification: Notification) {
itemWidth = CGFloat(300)
if window.width <= itemWidth {
GridItem(.flexible()), GridItem(.flexible())
} else if window.width <= itemWidth * 2 {
GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())
} else if window.width <= itemWidth * 3 {
GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())
}
...
}
我将如何使用 SwiftUI 实现这样的观察者?
如果您想在调整视图大小时更新
@State
或 @ObservedObject
变量,可以使用 onAppear(perform:) 使用初始视图大小更新变量,并使用 onChange(of:perform:) 来检测视图大小变化:
struct MyView: View {
@State private var size: CGSize = .zero
var body: some View {
GeometryReader { geometry in
ZStack {
Text("Hello World")
}.onAppear {
size = geometry.size
}.onChange(of: geometry.size) { newSize in
size = newSize
}
}
}
}
相当于监听窗口大小的 SwiftUI 可能会使用
GeometryReader
。在您的示例中,您可以读取大小并根据其宽度读数动态决定列:
struct NetworkGrid: View {
var networks: [Network]
func columnsForWidth(width: CGFloat) -> [GridItem] {
print("Columns for width: \(width)")
return Array(repeating: GridItem(.flexible()), count: Int(width) / 100)
}
var body: some View {
GeometryReader { geometry in
ScrollView {
LazyVGrid(columns: columnsForWidth(width: geometry.size.width)) {
ForEach(networks) { network in
NetworkCard(network: network)
}
}
}
}
}
}
无需 GeometryReader 也可以实现此目的:
let networkCardSize = 200
ScrollView(.vertical) {
LazyVGrid(
columns: [
// Move an item across rows iff it has enough space
// to contain *just* the item (+ padding).
GridItem(.adaptive(minimum: networkCardSize, maximum: networkCardSize)
],
alignment: .leading,
spacing: 0
) {
ForEach(networks) { network in
NetworkCard(network: network)
.padding(10)
}
}
}
spacing
没有按我的预期工作,但可能有一个很好的解决方案。我也不确定动态大小的元素在这里的表现如何。静态尺寸很棒!