Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my UIButton.tintColor not working?

Tags:

ios

uibutton

People also ask

How do I change the button color of an image programmatically in Swift?

If you are setting the image for a button, just go to attributes inspector and change the button type to system. Then set the image and change the tint color. The color of the image will change.

What is tint color in iOS?

What is tintColor? tintColor is a variable in UIView that returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself). If no non-default value is found, a system-defined color is returned (the shiny blue you always see).


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


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.


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;

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

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

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.