I noticed that when I place a white or black UIImage
into a UISegmentedControl
it automatically color masks it to match the tint of the segmented control. I thought this was really cool, and was wondering if I could do this elsewhere as well. For example, I have a bunch of buttons that have a uniform shape but varied colors. Instead of making a PNG for each button, could I somehow use this color masking to use the same image for all of them but then set a tint color or something to change their actual color?

- 16,233
- 18
- 112
- 180

- 5,013
- 4
- 29
- 37
-
Can you post the image that you want to use and also image for desired result? – Pratyusha Terli Nov 07 '13 at 06:32
-
this do the same from interface builder http://stackoverflow.com/a/25179217/2051381 – Chuy47 Feb 18 '16 at 21:44
22 Answers
As of iOS 7, there is a new method on UIImage
to specify the rendering mode. Using the rendering mode UIImageRenderingModeAlwaysTemplate
will allow the image color to be controlled by the button's tint color.
Objective-C
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [[UIImage imageNamed:@"image_name"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal];
button.tintColor = [UIColor redColor];
Swift
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red

- 15,419
- 6
- 50
- 75
-
20
-
4@M.Othman only works for me when I use button.tintColor and not button.imageview.tintColor – pnizzle Jul 01 '15 at 00:28
-
35Just remember to set image render mode on the image in xcassets per @hashier – netwire Jul 29 '15 at 21:03
-
29Creating a UIButton of type UIButtonTypeSystem will automatically honor the tintColor you set. – carloshwa Nov 03 '15 at 07:14
-
6If your `tintColor` is too dark, make sure `adjustsImageWhenHighlighted` is NO. (Or in IB: "Highlighted Adjusts Image" is unchecked.) – Jason Moore Nov 26 '15 at 19:47
-
@JasonMoore what an awesome comment! You just made my day, I didn't know why my `UIButton` was changing its `tintColor` and it was that! Thank you – Alejandro Iván Mar 31 '16 at 16:49
-
It is working for setImage for button but not for background image. And your efforts rewarded in advance – Raj Aggrawal Aug 25 '16 at 10:38
-
Just realized my image is actually an emoji... this is not going to work – William Entriken Feb 25 '17 at 02:56
As Ric already mentioned in his post you can set the render mode in code, you can also do this directly in the image catalog, see attached image below. Just set the Render As
to Template Image
Caveat I have had problems with iOS 7 and this approach. So if you use iOS 7 as well you might want to do it in code as well to be sure, as described here.
-
1As a side note, the image file should be black and transparent (not b&w) – Axel Guilmin Feb 25 '16 at 16:02
-
6@AxelGuilmin not really. Your image can be any color you want and transparent. Any color different than transparent **will be converted to a generic one** and then it will be tinted with black color by default. – Alejandro Iván Mar 31 '16 at 16:51
Custom Buttons appear in their respective image colors. Setting the button type to "System" in the storyboard (or to UIButtonTypeSystem
in code), will render the button's image with the default tint color.
(tested on iOS9, Xcode 7.3)

- 9,329
- 4
- 47
- 54
-
2Which can be avoided with the alwaysTemplate and tintColor methods suggested here. With the advantage of the .system button being you get the change in color with touch down on the tap. – Chris Prince Feb 08 '18 at 19:58
You must set the image rendering mode to UIImageRenderingModeAlwaysTemplate
in order to have the tintColor
affect the UIImage. Here is the solution in Swift:
let image = UIImage(named: "image-name")
let button = UIButton()
button.setImage(image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
button.tintColor = UIColor.whiteColor()
SWIFT 4x
button.setImage(image.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
button.tintColor = UIColor.blue

- 162
- 11

- 1,461
- 14
- 8
If you have a custom button with a background image.You can set the tint color of your button and override the image with following .
In assets select the button background you want to set tint color.
In the attribute inspector of the image set the value render as to "Template Image"
Now whenever you setbutton.tintColor = UIColor.red
you button will be shown in red.

- 11,644
- 6
- 63
- 79
-
if you are here you can go to this link: https://useyourloaf.com/blog/styling-buttons-using-the-asset-catalog/ and go down to template images (to serve as an explanation) – cspam May 22 '17 at 14:29
-
This is a better solution as it reduces code and pretty much does the same thing as the accepted answer. – DS. Apr 29 '18 at 23:29
-
I think this should be the correct one. less code and easy to handle. – Umair_UAS Apr 02 '20 at 07:57
In Swift you can do that like so:
var exampleImage = UIImage(named: "ExampleImage.png")?.imageWithRenderingMode(.AlwaysTemplate)
Then in your viewDidLoad
exampleButtonOutlet.setImage(exampleImage, forState: UIControlState.Normal)
And to modify the color
exampleButtonOutlet.tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) //your color
EDIT Xcode 8
Now you can also just the rendering mode of the image in your .xcassets to Template Image and then you don't need to specifically declare it in the var exampleImage
anymore

- 1,448
- 16
- 23
Not sure exactly what you want but this category method will mask a UIImage with a specified color so you can have a single image and change its color to whatever you want.
ImageUtils.h
- (UIImage *) maskWithColor:(UIColor *)color;
ImageUtils.m
-(UIImage *) maskWithColor:(UIColor *)color
{
CGImageRef maskImage = self.CGImage;
CGFloat width = self.size.width;
CGFloat height = self.size.height;
CGRect bounds = CGRectMake(0,0,width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClipToMask(bitmapContext, bounds, maskImage);
CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
CGContextFillRect(bitmapContext, bounds);
CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
UIImage *coloredImage = [UIImage imageWithCGImage:cImage];
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cImage);
return coloredImage;
}
Import the ImageUtils category and do something like this...
#import "ImageUtils.h"
...
UIImage *icon = [UIImage imageNamed:ICON_IMAGE];
UIImage *redIcon = [icon maskWithColor:UIColor.redColor];
UIImage *blueIcon = [icon maskWithColor:UIColor.blueColor];

- 11,254
- 3
- 32
- 60
-
3Use `kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast` on `CGBitmapContextCreate` – Luis Ascorbe Feb 04 '14 at 08:26
-
4Nice, but needs to be tweaked for Retina graphics... When I run this on a Retina image, it returns me a non-retina one. – jowie Mar 26 '14 at 14:08
-
To support Retina devices you could use the scale property of the `UIDevice` class: `CGFloat scale = [UIScreen mainScreen].scale; CGFloat width = self.size.width * scale; CGFloat height = self.size.height * scale;` The `scale` property exists as of iOS 4.0. – Philipp Frischmuth Jan 08 '15 at 12:29
-
You also need to use the `scale` value when the `UIImage` is created: `UIImage *coloredImage = [UIImage imageWithCGImage:cImage scale:scale orientation:self.imageOrientation];` – Philipp Frischmuth Jan 08 '15 at 12:42
Swift 4 with customType:
let button = UIButton(frame: aRectHere)
let buttonImage = UIImage(named: "imageName")
button.setImage(buttonImage?.withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white

- 323
- 4
- 16
Swift 3:
This solution could be comfortable if you have already setted your image through xCode interface builder. Basically you have one extension to colorize an image:
extension UIImage {
public func image(withTintColor color: UIColor) -> UIImage{
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context: CGContext = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: self.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(CGBlendMode.normal)
let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
context.clip(to: rect, mask: self.cgImage!)
color.setFill()
context.fill(rect)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
Then , you can prepare this UIButton extension to colorize the image for a particular state:
extension UIButton {
func imageWith(color:UIColor, for: UIControlState) {
if let imageForState = self.image(for: state) {
self.image(for: .normal)?.withRenderingMode(.alwaysTemplate)
let colorizedImage = imageForState.image(withTintColor: color)
self.setImage(colorizedImage, for: state)
}
}
}
Usage:
myButton.imageWith(.red, for: .normal)
P.S. (working good also in table cells, you don't need to call setNeedDisplay()
method, the change of the color is immediate due to the UIImage extension..

- 34,887
- 11
- 106
- 133
For Xamarin.iOS (C#):
UIButton messagesButton = new UIButton(UIButtonType.Custom);
UIImage icon = UIImage.FromBundle("Images/icon.png");
messagesButton.SetImage(icon.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal);
messagesButton.TintColor = UIColor.White;
messagesButton.Frame = new RectangleF(0, 0, 25, 25);

- 3,928
- 3
- 47
- 49

- 5,730
- 1
- 39
- 45
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red
If you are setting UIButton.tintColor
by UIColor(r:g:b:alpha:)
, remember to divide values by 255
. Those RGB values should be in between 0 and 1.

- 2,269
- 31
- 42
If you're arriving here after iOS 15 and you're using the new UIButton.Configuration
APIs, then you might need to do it via the imageColorTransformer
.
Looks like this:
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in .green }
For convenience, you can create an extension:
extension UIButton.Configuration {
func imageColor(_ color: UIColor) -> UIButton.Configuration {
var configuration = self
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in color }
return configuration
}
}
// Usage:
configuration = configuration.imageColor(.green)
As with the other answers, the image itself has to be "Render As - Template Image" in the Xcode Assets, or in code image.withRenderingMode(.alwaysTemplate)
BONUS TIP:
What if you want the image color to change when the button is highlighted? Then your configuration extension can look like this:
func imageColor(whenNormal: UIColor,
whenHighlighted: UIColor,
isHighlighted: Bool) -> UIButton.Configuration {
var configuration = self
configuration.imageColorTransformer = UIConfigurationColorTransformer { _ in
isHighlighted ? whenHighlighted : whenNormal
}
return configuration
}
And this itself has to be called from a configurationUpdateHandler
context, like this:
someButton.configurationUpdateHandler = { button in
guard var configuration = button.configuration else { return }
configuration.image = UIImage(named: "some_image")
configuration = configuration.imageColor(whenNormal: .green,
whenHighlighted: .green.withAlphaComponent(0.7),
isHighlighted: button.isHighlighted)
button.configuration = configuration
}
Note that the configurationUpdateHandler
is also where you can actually define a different image based on button state(s).

- 8,465
- 6
- 41
- 47
-
-
1This answer should be way up to the top. Specifically the detail about "Render As - Template Image" eluded me. – Jonny Apr 07 '23 at 11:11
If you want to manually mask your image, here is updated code that works with retina screens
- (UIImage *)maskWithColor:(UIColor *)color
{
CGImageRef maskImage = self.CGImage;
CGFloat width = self.size.width * self.scale;
CGFloat height = self.size.height * self.scale;
CGRect bounds = CGRectMake(0,0,width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextClipToMask(bitmapContext, bounds, maskImage);
CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
CGContextFillRect(bitmapContext, bounds);
CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
UIImage *coloredImage = [UIImage imageWithCGImage:cImage scale:self.scale orientation:self.imageOrientation];
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(cImage);
return coloredImage;
}

- 4,246
- 2
- 32
- 35
You Should Try
After Setting The Frame
NSArray *arr10 =[NSArray arrayWithObjects:btn1,btn2,nil];
for(UIButton *btn10 in arr10)
{
CAGradientLayer *btnGradient2 = [CAGradientLayer layer];
btnGradient2.frame = btn10.bounds;
btnGradient2.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:151.0/255.0f green:206.0/255.5 blue:99.0/255.0 alpha:1] CGColor],
(id)[[UIColor colorWithRed:126.0/255.0f green:192.0/255.5 blue:65.0/255.0 alpha:1]CGColor],
nil];
[btn10.layer insertSublayer:btnGradient2 atIndex:0];
}

- 529
- 1
- 4
- 9
Swift 3.0
let image = UIImage(named:"NoConnection")!
warningButton = UIButton(type: .system)
warningButton.setImage(image, for: .normal)
warningButton.tintColor = UIColor.lightText
warningButton.frame = CGRect(origin: CGPoint(x:-100,y:0), size: CGSize(width: 59, height: 56))
self.addSubview(warningButton)

- 2,139
- 1
- 16
- 28
Change button image or image view tint color Swift :
btn.imageView?.image = btn.imageView?.image?.withRenderingMode(.alwaysTemplate)
btn.imageView?.tintColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

- 7,431
- 5
- 54
- 81

- 1,118
- 10
- 11
To set white colour of the image(arrow icon) on the button, we're using:
let imageOnButton = UIImage(named: "navForwardArrow")?.imageWithColor(color: UIColor.white)
button.setImage(imageOnButton, for: .normal)
Known issue: The icon looses its white colour while the button is pressed.

- 1,851
- 22
- 18
Change button image or image view tint color Swift :
let image = UIImage(named: "map")
mapButton.setImage(image!.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
mapButton.tintColor = UIColor.black

- 731
- 9
- 7

- 11
- 2
None of above worked for me, because tint was cleared after click. I had to use
button.setImageTintColor(Palette.darkGray(), for: UIControlState())

- 920
- 9
- 23
I had a problem with masking image in highlighted state. I didn't want it to happen. If You have the same problem, check this out: adjustsImageWhenHighlighted = false

- 71
- 1
- 2
iOS 15+
If you have a UIButton based on an SF symbol and you created the button with a configuration, you can update the color after the fact as follows:
if var config = myButton.configuration {
let image = config.image?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [.systemBlue]))
config.image = image
myButton.configuration = config
myButton.setNeedsUpdateConfiguration()
}
Alternatively if you're trying to update the color of a button image you pulls from your app's assets catalog, this may do it:
if var config = myButton.configuration {
let image = config.image?.withTintColor(.systemBlue, renderingMode: .alwaysTemplate)
config.image = image
myButton.configuration = config
myButton.setNeedsUpdateConfiguration()
}

- 12,255
- 11
- 57
- 75
Swift 5, using Storyboard:
Make sure you don't set the foreground color of the UIButton in Attributes Inspector(see image).
Otherwise, if you set it, it will ignore all programmatic configurations!
Just let it be default
.

- 3,607
- 3
- 17
- 41