45

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)
pkamb
  • 33,281
  • 23
  • 160
  • 191
francisfeng
  • 704
  • 1
  • 8
  • 22
  • iOS 15 in 2021 has new multi-colored icons in SF Symbols 3: https://stackoverflow.com/questions/69304679/sf-symbols-hierarchical-palette-and-multicolor-rendering-mode-colors – pkamb Sep 28 '21 at 23:13

7 Answers7

80

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
ShigaSuresh
  • 1,598
  • 17
  • 19
45

Use:

let icon = UIImageView(image: iconImage.withRenderingMode(.alwaysTemplate))
icon.tintColor = .red
Yonat
  • 4,382
  • 2
  • 28
  • 37
10
let iconImage = UIImage(systemName: "chevron.right",
                                            withConfiguration: UIImage.SymbolConfiguration(pointSize: 16, weight: .regular, scale: .medium))?.withTintColor(.systemGreen)
Nikolay Ukolov
  • 111
  • 1
  • 3
9
muteCall.setImage(UIImage(systemName: "mic.slash")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)

Set rendering to always original

qwaswsa
  • 175
  • 1
  • 8
  • 2
    Seems this is replicating the solution from the most upvoted answer - https://stackoverflow.com/a/61371425/1974224 – Cristik Sep 26 '21 at 05:54
7
let image = UIImage(systemName: "arrow.up.circle.fill")?.withTintColor(.black, renderingMode: .alwaysOriginal)
button.setImage(image, for: .normal)
  • 3
    Seems this is replicating the solution from the most upvoted answer - https://stackoverflow.com/a/61371425/1974224 – Cristik Sep 26 '21 at 05:54
5

A small addition. I'm glad if it's of any help to 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)
Vladimir Moor
  • 81
  • 1
  • 5
2
@IBOutlet weak var downButton: UIButton!
downButton.setImage(UIImage(systemName: "chevron.down")?.withTintColor(.red, renderingMode: .alwaysOriginal), for: .normal)
  • Can you add details about how the proposed code addresses the problem? Code-only answers are not very helpful, neither for the asker, nor others. – Cristik Sep 25 '21 at 17:00
  • This answer was flagged as [Low Quality](https://stackoverflow.com/help/review-low-quality) and could benefit from an explanation. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Code only answers are **not considered good answers** and are likely to be downvoted and/or deleted because they are **less useful** to a community of learners. It's only obvious to you. Explain what it does, and how it's different / better than existing answers (if there are any). [From Review](https://stackoverflow.com/review/low-quality-posts/29904629) – Trenton McKinney Sep 25 '21 at 22:28
  • 2
    Also, seems this is replicating the solution from the most upvoted answer - https://stackoverflow.com/a/61371425/1974224 – Cristik Sep 26 '21 at 05:54