UIButton setTitleColor:forState: question
Why does the following code work...
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[signInBtn setTitleColor:[UIColor开发者_JAVA技巧 blackColor] forState:UIControlStateDisabled];
while this does not?
[signInBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted|UIControlStateDisabled];
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
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.)
It could be a bug. Try changing bitmask with unexpected value like UIControlStateHighlighted
& UIControlStateDisabled
, and it make all the state color the same.
精彩评论