我想知道如何动态地创建多个SwiftUI形状(如 矩形).
如何从这样的东西。
struct ContentView: View {
var body: some View {
Rectangle()
.fill(Color.blue)
.frame(width: 100, height: 100)
}
}
到这样的东西。
struct ContentView: View {
var body: some View {
VStack {
for index 1...10 {
Rectangle()
.fill(Color.black)
.frame(width:100, height: 100)
}
}
}
}
解决方案:
你可以使用SwiftUI的 ForEach 通过提供一个范围的视图。
struct ContentView: View {
var body: some View {
VStack {
ForEach(0 ..< 10) { _ in
Rectangle()
.fill(Color.black)
.frame(width:100, height: 100)
}
}
}
}
本文来自投稿,不代表运维实战侠立场,如若转载,请注明出处:https://www.shizhanxia.com/894.html