francisfeng
francisfeng

Reputation: 808

How to change SF Symbol icon color in UIKit?

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)

Upvotes: 54

Views: 28516

Answers (3)

Vladimir Moor
Vladimir Moor

Reputation: 371

A small addition. I'm glad if it helps anyone. We can combine not only different colours, but also different sizes and weight. For example you can do this:

let colorsConfig = UIImage.SymbolConfiguration(paletteColors: [.white, .magenta])
let sizeConfig = UIImage.SymbolConfiguration(pointSize: 30, weight: .bold)
let image = UIImage(systemName: "plus.circle.fill", withConfiguration: colorsConfig.applying(sizeConfig))
button.setImage(image, for: .normal)

Upvotes: 21

ShigaSuresh
ShigaSuresh

Reputation: 1768

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

Upvotes: 91

Yonat
Yonat

Reputation: 4608

Use:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red

Upvotes: 46

Related Questions