Personal website of developer Filip Stefansson / Archive

Add tint color to UIButtons with UITintedButton

When you create a UIBarButtonItem there’s an option to set the tint color, which will change every pixel of the image with an alpha over 0 to that color.

When you set the tint color of a UIButton, however, nothing happens. This is where UITintedButton comes in handy.

I’ve created a UIButton category wich adds four new methods:

-(void)setImageTintColor:(UIColor *)color;
-(void)setBackgroundTintColor:(UIColor *)color forState:(UIControlState)state;
+(void)tintButtonImages:(NSArray *)buttons withColor:(UIColor *)color;
+(void)tintButtonBackgrounds:(NSArray *)buttons withColor:(UIColor *)color forState:(UIControlState)state;

The first two are instance methods, that changes the tint of a single image. The other two are class methods, that allows you to change the tint of several buttons.

Usage

#import UIButton+tintImage.h

// Tint single buttons
[button setImageTintColor:[UIColor redColor]];
[button setBackgroundTintColor:[UIColor redColor] forState:UIControlStateNormal];

// Tint multiple buttons
[UIButton tintButtonImages:@[button1, button2, button3] withColor:[UIColor redColor]];
[UIButton tintButtonBackgrounds:@[button1, button2, button3] withColor:[UIColor redColor] forState:UIControlStateNormal];

Github and download

Feel free to fork and improve. Here’s a link to the repo: UITintedButton.

More articles →