Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton setTitleColor:forState: question

Why does the following code work...

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];

while this does not?

[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted|UIControlStateDisabled];
like image 665
bioffe Avatar asked Dec 06 '10 20:12

bioffe


2 Answers

I know this is an old question, but these answers aren't correct.

When you set each separately you are saying the state property should be UIControlStateHighlighted OR UIControlStateDisabled but NOT both

When you bitwise or them together you are stating they must BOTH be set in the state property. Meaning UIControlStateHighlighted AND UIControlStateDisabled are set in the state property.

The example code below perfectly illustrates my point. If you disagree run it for yourself.

[button setTitle:@"highlighted and selected" forState:UIControlStateHighlighted | UIControlStateSelected];
[button setTitle:@"Highlighted only" forState:UIControlStateHighlighted];
[button setTitle:@"Selected only" forState:UIControlStateSelected];
[button setTitle:@"Normal" forState:UIControlStateNormal];

NSLog(@"Normal title: %@", [[button titleLabel] text]); // prints title: Normal

[button setSelected:YES];

NSLog(@"Selected title: %@", [[button titleLabel] text]); // prints title: Selected only 

[button setSelected:NO];
[button setHighlighted:YES];

NSLog(@"highlighted title: %@", [[button titleLabel] text]); // prints title: Highlighted only

[button setSelected:YES];

NSLog(@"highlighted and selected title: %@", [[button titleLabel] text]); // prints title: highlighted and selected
like image 78
Patrick Hernandez Avatar answered Nov 19 '22 17:11

Patrick Hernandez


Because the setTitleColor:forState: method can only accept a known UIControlState and you're ORing two UIControlState values together.

Each UIControlState is (at a low level) most likely a simple integer constant.

Update:

It's a bitmask, which makes it a rather more odd that it doesn't work, but my point still stands. (It is leaning alarmingly to one side and wobbling dangerously though.)

like image 31
John Parker Avatar answered Nov 19 '22 17:11

John Parker