Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton Default Title Color iOS7

Tags:

ios

uibutton

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.

like image 933
Rohan Hamer Avatar asked Sep 23 '13 05:09

Rohan Hamer


Video Answer


3 Answers

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];
like image 87
user2282219 Avatar answered Nov 15 '22 19:11

user2282219


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.

like image 43
fishinear Avatar answered Nov 15 '22 19:11

fishinear


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

like image 24
user3441734 Avatar answered Nov 15 '22 20:11

user3441734