60

My build target is set for IOS5 which is where I understand UIButton.tintColor was introduced...

I have this in my viewDidLoad for the View Controller

[timelineButton setTintColor:[UIColor blackColor]];
[timelineButton setTitle:@"test" forState:UIControlStateNormal];

The text changes properly but the button isn't black?

Thanks!

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
sayguh
  • 2,540
  • 4
  • 27
  • 33

14 Answers14

70

According to the documentation:

This property is not valid for all button types.

You need to switch your buttonType to another one of these. (Unfortunately, the documentation does not specify which specific button types support this property.)

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • 1
    Just using a standard rounded rectangle type. Doesn't work for that? – sayguh Jun 16 '12 at 18:40
  • 2
    It does work for me in Storyboard / Interface Builder : set the button type to 'system', then set the background of the view (of the UIButton) to the desired color - works perfect! – Ronny Webers Jan 10 '14 at 00:19
  • 12
    Note for iOS 7: Using `UIButtonTypeSystem`, the button will automatically use `tintColor` for the title label's `textColor`. – Patrick Pijnappel May 04 '14 at 01:25
  • 3
    Note for iOS 7: UIButtonTypeSystem trick works for textColor and imageColor – jaime Aug 14 '14 at 14:38
  • 2
    **Swift 3** For anyone doing it programatically: `var button = UIButton(type: .system)` ` button.tintColor = UIColor.blue` – SpaceX Aug 15 '17 at 08:28
42

Make sure your button "type" is set to System. If it is set to Custom it could be the reason the color of your button isn't changing. This is what happened to me and changing the type to System fixed it.

Jesse S.
  • 783
  • 7
  • 11
8

It tint your highlighted State color. When you tap/click on the UIButton the color specified with tintColor appears as long as you hold the tap/click on the UIButton.

resetButton.tintColor = [UIColor colorWithRed:0.764 green:1.000 blue:0.000 alpha:1.000];

The button is white in normal state. But if I tap on the button the color turns red, but only then.

IF you need to change the button so it looks like a red or blue one in the UIControlStateNormal then

Change the UIButtonType to UIButtonTypeCustom in Interface Builder or programmatically with

 UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];

Change the attributes on your own and recreate the rounded corners

resetButton.backgroundColor = [UIColor redColor];
resetButton.layer.borderColor = [UIColor blackColor].CGColor;
resetButton.layer.borderWidth = 0.5f;
resetButton.layer.cornerRadius = 10.0f;
Binarian
  • 12,296
  • 8
  • 53
  • 84
  • 2
    And don't forget to include #import for CALayer declarations. – zero0cool Jan 28 '13 at 15:03
  • Readers should be cautioned that this isn't enough -- as it is, the button does not react when you touch it. You need to hook up some additional events to change the colors when pressed (see alexqinbj's answer below). – Joe Strout Dec 19 '13 at 20:26
7

As stated in other answers tint does not work for Custom Button types. Make sure you explicitly declare the button type. Do not just use [UIButton alloc] init]

This will work:

UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[mybutton setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
mybutton.tintColor = [ODTheme blueTintColor];
Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40
5

I found 3 things must be followed.

1. Render image as Default

Go to Images.xcassets > Your Image > Show the Attributes inspector > Render As > Default

render as default

2. Make sure your button type is System

3. Now change button's tintColor.

Done.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
4

Today, I also meet this problem. I use delegate to solve it.

[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonPressReset:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

-(void)buttonPress:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor greenColor]];
NSLog(@"buttonPressed");
}

-(void)buttonPressReset:(id)sender{
UIButton* button = (UIButton*)sender;
[button setBackgroundColor:[UIColor redColor]];
NSLog(@"buttonPressReset");
}
alexqinbj
  • 1,091
  • 3
  • 13
  • 27
  • This isn't quite complete -- you should also add UIControlEventTouchDragEnter to the events that trigger buttonPress, and UIControlEventTouchDragExit to the events that trigger buttonPressReset. – Joe Strout Dec 19 '13 at 20:31
3

Should try this method:

- (void)setTitleColor:(UIColor *)color
             forState:(UIControlState)state
Undo
  • 25,519
  • 37
  • 106
  • 129
Zverusha
  • 317
  • 3
  • 5
3

In iOS 13 you can tint the image and then set it to the button:

let coloredImage = UIImage().withTintColor(UIColor.red)
UIButton().setImage(coloredImage, for: .normal)
rockdaswift
  • 9,613
  • 5
  • 40
  • 46
2

Simply set your UIButton to type System in Storyboard.

enter image description here

And then in code just use:

myButton.tintColor = UIColor.whiteColor()
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
2

If your UIButton type is system then and then only tint colour property is work. So first you need to set your button type to the system then apply tint colour for the specific state.

let btn = UIButton.init(type: .system)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
Bhavesh Patel
  • 596
  • 4
  • 17
  • Now it will compile, but I don't think your answer is adding anything new to the other answers, you are just repeating what others said 5 years ago. – Mojo66 Sep 26 '18 at 13:10
1

To change the color of the Button text you can use:

resetButton.setTitleColor(UIColor.blackColor(), forState: .Normal)

Or OBJ-C:

- (void)setTitleColor:(UIColor *)color
         forState:(UIControlState)state

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instm/UIButton/setTitleColor:forState:

0

Simply use:

[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
[yourButton setTintColor:[UIColor blackColor]];
Zoe
  • 27,060
  • 21
  • 118
  • 148
Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
0

Swift 4 :

yourButton.setTitleColor(UIColor.white, for: .normal)
pableiros
  • 14,932
  • 12
  • 99
  • 105
Boris Legovic
  • 200
  • 2
  • 11
0

I couldn't really change the button type to System as I was inheriting from a closed internal library.

So, I changed the appearance API for UIButton

UIView.appearance(whenContainedInInstancesOf: [UIButton.self]).tintColor =  .white
Ali
  • 2,427
  • 22
  • 25