To color an image with tintColor, the image has to be a template: set Render As to Template Image in the asset catalog, or call withRenderingMode(.alwaysTemplate) in code, then set the image view's tintColor. SF Symbols take the tint color as they are. Template mode only works where a view applies the tint. When you need an image with the color baked in, use withTintColor(_:renderingMode:) with .alwaysOriginal.
SWIFT TIPS · UIKIT
First posted on October 24, 2019, as "Change image color with set attributes or programmatically". Rewritten on November 18, 2026. The rendering test was run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1. The view controller was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 as the deployment target, but not run on a device.
In 2019 I needed one icon in different colors without adding a copy of the image for each color. The post showed two ways: set the image's Render As option to Template Image in the asset catalog and pick a tint on the image view in the storyboard, or do the same in code with withRenderingMode(.alwaysTemplate) and tintColor.
Both ways are still right. The code line in the post had a small error, and there's one case where template mode doesn't do anything, which I checked by rendering the images and reading their pixels.
Where template mode works, and where it doesn't
The test starts with a plain black 20-point square, like a black icon in an asset catalog. It shows the image in different ways with red as the tint, renders each one, and reads the color of the middle pixel:
image view, original image, tint red: r 0 g 0 b 0
image view, template image, tint red: r 255 g 0 b 0
withTintColor(.red, .alwaysOriginal): r 255 g 0 b 0
image view, SF Symbol, tint red: r 255 g 0 b 0
template image drawn with draw(at:): r 0 g 0 b 0
- An original image ignores the tint. In an image view with a red tint, the black image stayed black.
- A template image takes the tint. The same image with
.alwaysTemplatecame out red in the same image view. - A baked-in color works anywhere.
withTintColor(.red, renderingMode: .alwaysOriginal)gave a red image. - SF Symbols take the tint. A system symbol in an image view came out red with no extra step.
- Drawing a template image yourself doesn't tint it. The template image drawn straight into a graphics context with
draw(at:)stayed black. Template mode is an instruction to the view that shows the image. When there's no such view, for example when you draw the image into another image or pass it to code that draws it, use the baked-in version.
Coloring icons in code
import UIKit
final class TintedIconsViewController: UIViewController {
private let assetIconView = UIImageView()
private let symbolView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// An image from the asset catalog: draw it as a template so tintColor colors it.
// (Or set Render As to Template Image in the asset's Attributes inspector.)
assetIconView.image = UIImage(named: "bell")?.withRenderingMode(.alwaysTemplate)
assetIconView.tintColor = .systemGray
assetIconView.contentMode = .scaleAspectFit
// SF Symbols take the tint color without any extra step.
symbolView.image = UIImage(systemName: "bell.fill")
symbolView.tintColor = .systemOrange
symbolView.preferredSymbolConfiguration = UIImage.SymbolConfiguration(textStyle: .title2)
symbolView.contentMode = .scaleAspectFit
}
}
extension UIImage {
/// A copy with the color baked in, for places that don't apply tintColor.
func colored(_ color: UIColor) -> UIImage {
withTintColor(color, renderingMode: .alwaysOriginal)
}
}
How it works
- Template images keep only the shape. Apple's documentation for
.alwaysTemplatesays the image is drawn as a template, ignoring its color information. The visible pixels take the tint color. That's why a black PNG can become any color, and also why a multicolored image turns into a single-color silhouette. - The tint is inherited. A view's
tintColorcomes from its superview unless you set it, so one tint on a container colors every template image inside it. The 2019 code set it on the image view directly, which is clearer when only one icon should change. - SF Symbols. System symbols take the tint color in an image view, as the test shows, and
preferredSymbolConfigurationsizes them to a text style, so the icon matches the text next to it. - Baked-in colors.
colored(_:)wrapswithTintColor(_:renderingMode:). Apple's documentation says that for bitmap images it draws the tint color and then the image contents with a mask, so the result is the image's shape in that color, independent of any view.
The 2019 code line
The post set the image with imageView.image = imageView.image.withRenderingMode(.alwaysTemplate). Type-checked with Xcode 26.2, it stops with "value of optional type 'UIImage?' must be unwrapped to refer to member 'withRenderingMode' of wrapped base type 'UIImage'", because an image view's image can be nil. Adding a question mark, imageView.image?.withRenderingMode(.alwaysTemplate), fixes it, and does nothing when there's no image.
Asset catalog or code?
If an image is only ever used as a tinted icon, set Render As to Template Image in the asset catalog. Then every place that uses it, including storyboards, gets a template without extra code. Use withRenderingMode in code when the same image is shown in its original colors in one place and tinted in another.
A quick check for your project
- Find icons that exist in several colored copies in the asset catalog. One template image and a tint can usually replace them.
- Search for
.alwaysTemplateon images that are drawn into a context or passed to other code. Those needwithTintColor(_:renderingMode:)instead. - Check your template icons in dark mode. A tint set to a fixed color, like the 2019 white and gray, doesn't change with the appearance. System colors such as
.systemGraydo.
What the 2019 version said
The original showed the Render As menu with Default, Original Image and Template Image, the asset's attributes with Template Image selected for an image named "qr", and the image view's Tint set to a custom color in the storyboard:



Keep reading on JR Devhub
Comments