71

I am trying to set an image tint in SwiftUI Image class

For UIKit, I can set image tint using

let image = UIImage(systemName: "cart")!.withTintColor(.blue)

but I cant find such settings in swiftui docs

Nic Wanavit
  • 2,363
  • 5
  • 19
  • 31

5 Answers5

104

On new swiftUI for set tint use:

Image("ImageName")
  .foregroundColor(.red)

Depending on the source image you may also need an additional rendering mode modifier:

Image("ImageName")
  .renderingMode(.template)
  .colorMultiply(.red)
// or
Image("ImageName")
  .colorMultiply(.blue)

And you can read this topic.

aturan23
  • 4,798
  • 4
  • 28
  • 52
  • 3
    Depending on the source image you may also need an additional rendering mode modifier: `Image("ImageName").renderingMode(.template).colorMultiply(.red)` – dead_can_dance Aug 17 '20 at 14:41
  • 2
    The problem with this solution is that depending on if you are in light or dark mode, template images may start as black and .colorMultiply will have no. color multiply gives different results based on the original color of the image. – bitwit Jan 25 '21 at 21:50
  • 1
    For some reason, if I use `renderingMode` + `colorMultiply` some colors where behaving weird (e.g. white was becoming blue). So I just replaced `colorMultiply` with `foregroundColor`. Final result: `Image("ImageName").renderingMode(.template). foregroundColor(.red)` – Caio Ambrosio Aug 21 '23 at 12:08
67

This worked for me,

Image("ImageName")
   .renderingMode(.template)
   .foregroundColor(.white)
Mona
  • 5,939
  • 3
  • 28
  • 33
11

Blending modes

For tinting, you can use the .blendMode modifier to composite your image with another image or with color. There are 20+ Photoshop-like blending modes in Xcode 14.0+. The most appropriate SwiftUI's blending modes for tinting are:

  • .softLight
  • .overlay
  • .luminosity
  • .screen

enter image description here

You'll get different results than you see here if you change the brightness and saturation of the blending color.

enter image description here

Here's the code:

struct ContentView: View {
    var body: some View {
        ZStack {
            Rectangle()
                .fill(Gradient(colors: [.red, .yellow]))
                .ignoresSafeArea()
            Image("XcodeIcon")
                .resizable()
                .scaledToFit()
                .blendMode(.softLight)
        }
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
8

SwiftUI 4.x:

Approach 1:

Image("your_image_name")
    .resizable()
    .renderingMode(.template)
    .foregroundColor(.white)

Approach 2:

Image("your_image_name")
  .resizable()
  .renderingMode(.template)
  .colorMultiply(.red) 
Rabel Ahmed
  • 1,131
  • 13
  • 12
3

The above answer is probably the best one although this would work too:

let uiImage = UIImage(systemName: "cart")!.withTintColor(.blue)
let image = Image(uiImage: uiImage)

Then you can use the constant named image with SwiftUI.

Todd
  • 500
  • 4
  • 10
  • You might need to declare `uiImage` globally. I did it like that and it worked for me. – Todd Aug 12 '20 at 03:22