Why is my UIButton.tintColor not working?
IosUibuttonIos Problem Overview
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!
Ios Solutions
Solution 1 - Ios
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.)
Solution 2 - Ios
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.
Solution 3 - Ios
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;
Solution 4 - Ios
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];
Solution 5 - Ios
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");
}
Solution 6 - Ios
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
2. Make sure your button type is System
3. Now change button's tintColor.
Done.
Solution 7 - Ios
Should try this method:
- (void)setTitleColor:(UIColor *)color
forState:(UIControlState)state
Solution 8 - Ios
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)
Solution 9 - Ios
Simply set your UIButton
to type System in Storyboard.
And then in code just use:
myButton.tintColor = UIColor.whiteColor()
Solution 10 - Ios
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];
Solution 11 - Ios
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
Solution 12 - Ios
Simply use:
[yourButton setBackgroundImage:[yourButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
[yourButton setTintColor:[UIColor blackColor]];
Solution 13 - Ios
Swift 4 :
yourButton.setTitleColor(UIColor.white, for: .normal)