UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: System Image "rectangle.fill need to be able to switch between two colors

Forums > SwiftUI

I have been stuck on this issue for a few days now and I am seeking some help on this issue

I got a system Image "rectangle.fill" that I would like to use the color blue instead of black. Can someone please give me some guidance on this issue please

struct ContentView: View {

@State var imageName = "rectangle"
@State private var pressed = false
@State private var changeColor = Color.blue // ???
var body: some View {
    ZStack {
        //LoggingOn()
        Color.gray
            VStack {
                Spacer()
                HStack {
                    Image(systemName: self.imageName)
                    .padding(.leading)

                    Spacer()
                    Button("tap me") {
       self.imageName = self.imageName == "rectangle" ? "rectangle.fill" : "rectangle"
//self.changeColor = self.changeColor == .foregroundColor(.black) ? .foregroundColor(.blue) : .foregroundColor(.black)
                        self.pressed.toggle()
                        if self.pressed {

                        }

                    }
                    Spacer()

                    .padding(.leading)
                        //.padding(.horizontal)

                        .foregroundColor(.white)
                }
                      Spacer()

             }
       }

    .edgesIgnoringSafeArea(.all)
}

}

3      

To clarify, you want the color of the image to change when the button is tapped?

Something like this should work:

Image(systemName: self.imageName)
.foregroundColor(self.pressed ? .black : .gray)

4      

THANK YOU, I am going to go and apply it. DEEPLY Appriciated robert

3      

That did it, thank you again

3      

Happy to help!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.