SimpsonHuang
SimpsonHuang

Reputation: 53

How to use `Button` to add new `VStack` by using `SwiftUI`

I am using iOS 14 and Xcode 12 to learn SwiftUI.

I want to use Button to add new or more VStacks\.

Anyone know how to use the buttons and add a new VStack?

Any help would be appreciate. This is my code.

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

Upvotes: 0

Views: 1083

Answers (1)

not a brilliant idea, but if that's what you want to do, try this:

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")
                }
            }
    }
}

Upvotes: 1

Related Questions