WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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 Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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.