How to change SF Symbol icon color in UIKit?

Viewed 17772

In SwiftUI, you can change the icon's color using foregroundColor modifier:
Change the stroke/fill color of SF Symbol icon in SwiftUI?

Is there a way to change the color in UIKit? I looked up the documentation and didn't find anything related to it.

let configuration = UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium)
let iconImage = UIImage(systemName: "chevron.right", withConfiguration: configuration)
6 Answers

Use below code for changing SFSymbols icons color

let imageIcon = UIImage(systemName: "heart.fill")?.withTintColor(.red, renderingMode: .alwaysOriginal)
    imageView.image = imageIcon

Before

enter image description here

After

enter image description here

Use:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red
muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)

Set rendering to always original

let iconImage = UIImage(systemName: "chevron.right",
                                            withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))?.withTintColor(.systemGreen)
let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)
@IBOutlet weak var downButton: UIButton!
downButton.setImage(UIImage(systemName: "chevron.down")?.withTintColor(.red, renderingMode: .alwaysOriginal), for: .normal)
Related