How can I create a button with image in SwiftUI?

Viewed 47625

I created a button in SwiftUI with these line of codes:

Button(action: {
    print("button pressed")
}) {
    Image("marker")
}

but marker image automatically changes to blue color.

I want to use original image in button.

this is original marker.png:

enter image description here

but SwiftUI changes it to this:

enter image description here

I remember we have tintColor or something like this in UIButton, but I can't find it in SwiftUI.

4 Answers

Another way to set programmatically:-

var body: some View {
        Button(action: {
          print("button pressed")

        }) {
            Image("marker")
            .renderingMode(Image.TemplateRenderingMode?.init(Image.TemplateRenderingMode.original))
        }
    }

Go to the image and change the Render As "Original Image" enter image description here

You can try this:

var body: some View {
        Button(action: {
          print("button pressed")

        }) {
            Image("marker")
            .renderingMode(.original)
        }
    }

SwiftUI

var body: some View {
      HStack {
           Image(uiImage: UIImage(named: "Login")!)
                .renderingMode(.original)
                .font(.title)
                .foregroundColor(.blue)

           Text("Login")
                .font(.title)
                .foregroundColor(.white)
      }
}
Related