NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to change color for SFSymbol UIImage?

Forums > iOS

I would like to change the Map Pin.

By doing MKPinAnnotationView(...).image = UIImage(systemName: "gift")
I could change the Pin icon to other SF Symbols.

But I couldn't change the color..

I have tried

UIImage(systemName: "gift").withTintColor(.blue)

But there is no effect, I also have tried changing different rendering mode, it doesn't change either. I have seen others said it can be change by casting to UIImageView, however, the MKPinAnnotationView.image only accept UIImage Type

I have also tried using UIGraphicsImageRenderer with draw but it only adding to the background color, while I wish to change foregroundColor...

Is there anyway to change color?

Thank you very much!

1      

A bit of a guess here, but try UIImage(systemName: "gift")?.withTintColor(UIColor.blue)

You will be able to drop the UIColor before .blue if you're on a late enough version of Swift.

Bob

1      

Unfortuately it does not work..

1      

Did you try this?

Image(systemName: "gift") .foregroundColor(.blue)

Maybe this only a SwiftUI thing another thought maybe you could import SwiftUI in the file?

1      

Hmm

Yes and no...

Mapview.image is UIImage.
Unlike converting UIImage to Image, there is no direct way to convert Image to UIImage.

Being said that, I just found this View extension: https://stackoverflow.com/a/64005395 to convert it.

1      

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.