UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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!

3      

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

3      

Unfortuately it does not work..

3      

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?

3      

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.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.