SwiftUI question

Hello folks,

I have a very simple SwiftUI question. Here’s what I want to do:

  1. When the app launches, my view is filled with an image.
  2. There is a button.
  3. When I tap the button, I want the image to change.
  4. I want the image to toggle at each tap of the button.

I have only been partially successful.

Thanx,

ramesh.

Hi Nagar,

Try this out.

struct ContentView: View {
    @State private var toggleImage = false
    
    var body: some View {
        VStack(spacing: 40) {
            Image(systemName: toggleImage ? "person" : "person.fill")
                .resizable()
                .frame(width: 300, height: 300)
                .foregroundColor(toggleImage ? .blue : .red)
            
            Button(action: {
                self.toggleImage.toggle()
            }, label: {
                Text("Toggle Image")
            })
        }
    }
}

Thanx Chris. Appreciate the quick response.

No problem.

Does that all make sense?

By the way, the Button can also be done this way:

Button(action: {
    self.toggleImage.toggle()
}) {
    Text(toggleImage ? "Change Back" : "Change Image")
}

Yes it does - thanx Chris!