NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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

}

   

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)

1      

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

   

That did it, thank you again

   

Happy to help!

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.