I want to know how to change the Title Colour of a UIButton back to the default value.
I have a button that I change the title colour to indicate a something is on, like this;
[cell.offStateSwitch setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
Then when it is off, I want to change it back to the default colour.
I cant figure out how to get the default system colour for a UIButton. I have found lots of people doing this by forcing a particular RGB value, but that is not necessarily correct across different versions of iOS.
Set the colour to nil and it's behaviour will return to how it was before you changed it, including responding properly to alerts.
[myButton setTitleColor:nil forState:UIControlStateNormal];
I assume you mean you want to set the text back to the tintColor. If you just want the current tintColor, you can do:
[button setTitleColor:button.tintColor forState:UIControlStateNormal];
However, the tintColor is supposed to automatically change in certain circumstances, such as when an alert pops up. In order to achieve that, I think you need to define your own button:
@interface MyButton : UIButton
@end
@implementation MyButton
- (void) tintColorDidChange
{
[self setTitleColor:self.tintColor forState:UIControlStateNormal];
[super tintColorDidChange];
}
@end
In that case, I propose you use the selected state for you special color:
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
and set button.selected = YES
when "something is on", as you say.
button.tintColor = nil
or
[button setTintColor: null]
In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information.
This property has no default effect for buttons with type UIButtonTypeCustom. For custom buttons, you must implement any behavior related to tintColor yourself.
setting UIButton's tintColor to nil (null) will reset the color of its title
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With