【问题标题】:How to use `Button` to add new `VStack` by using `SwiftUI`如何使用 `Button` 通过 `SwiftUI` 添加新的 `VStack`
【发布时间】:2021-11-07 12:10:52
【问题描述】:

我正在使用 iOS 14 和 Xcode 12 来学习 SwiftUI。

我想用 Button 添加新的或更多的VStacks\

有人知道如何使用这些按钮并添加一个新的VStack吗?

任何帮助将不胜感激。 这是我的代码。

Button(action: {
   VStack{
         Text("hello")
   }
}, label: {
      Image(systemName: "plus.circle.fill")
           .foregroundColor(.black)
})

【问题讨论】:

    标签: ios swift button swiftui vstack


    【解决方案1】:

    这不是一个绝妙的主意,但如果你想这样做,试试这个:

    struct ContentView: View {
        @State private var vstacks = 0
        
        var body: some View {
                Button(action: { vstacks += 1 } ) {
                    Image(systemName: "plus.circle.fill").foregroundColor(.black)
                }
                ForEach(0..<vstacks, id: \.self) { vs in
                    VStack{
                        Text("hello vstack: \(vs) here")
                    }
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多