sayguh
sayguh

Reputation: 2550

Why is my UIButton.tintColor not working?

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!

Upvotes: 61

Views: 63610

Answers (14)

Ali
Ali

Reputation: 2487

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

Upvotes: 0

rockdaswift
rockdaswift

Reputation: 9983

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)

Upvotes: 3

Boris Legovic
Boris Legovic

Reputation: 220

Swift 4 :

yourButton.setTitleColor(UIColor.white, for: .normal)

Upvotes: 0

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

Simply use:

[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal]; 
[yourButton setTintColor:[UIColor blackColor]];

Upvotes: 0

Bhavesh Patel
Bhavesh Patel

Reputation: 596

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];

Upvotes: 2

Zaid Pathan
Zaid Pathan

Reputation: 16820

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.

Upvotes: 6

Arjon Bimmel
Arjon Bimmel

Reputation: 11

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:

Upvotes: 1

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61774

Simply set your UIButton to type System in Storyboard.

enter image description here

And then in code just use:

myButton.tintColor = UIColor.whiteColor()

Upvotes: 2

Zverusha
Zverusha

Reputation: 317

Should try this method:

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

Upvotes: 3

Jesse S.
Jesse S.

Reputation: 803

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.

Upvotes: 43

Robert Wagstaff
Robert Wagstaff

Reputation: 2674

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];

Upvotes: 7

alexqinbj
alexqinbj

Reputation: 1251

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");
}

Upvotes: 4

Binarian
Binarian

Reputation: 12446

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;

Upvotes: 8

Evan Mulawski
Evan Mulawski

Reputation: 55334

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.)

Upvotes: 70

Related Questions