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
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
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.
This worked for me,
Image("ImageName")
.renderingMode(.template)
.foregroundColor(.white)
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:
You'll get different results than you see here if you change the brightness and saturation of the blending color.
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)
}
}
}
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)
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.