I'm trying to set a UIButton
's titleColor
to a certain color when it is selected and when it is highlighted, however, I need to set the button to be selected when the user touches down on the UIButton
.
I've set it up like so:
[button setTitleColor:normalColor forState:UIControlStateNormal];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateHighlighted];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateSelected];
[button addTarget:self
action:@selector(action:)
forControlEvents:UIControlEventTouchDown];
But when the button gets selected in the action:
method using [senderButton setSelected:YES]
, it sets the titleColor
to normalColor
, rather than superDuperSpecialColor
, which it should be, as it's both highlighted AND selected.
Commenting out the setSelected:
call prevents the button from becoming and staying selected and commenting out the highlighted state color doesn't have any effect, it seems.
Will I have to add targets for UIControlEventTouchCancel
, UIControlEventTouchUpInside
and UIControlEventTouchUpOutside
in order to call setSelected:
after the highlight ends -or- change the titleColor
for UIControlStateNormal
to superDuperSpecialColor
when the button gets a touch?
On a side note, I would have liked to set the titleColor
like so:
[button setTitleColor:superDuperSpecialColor
forState:(UIControlStateHighlighted | UIControlStateSelected)];
But that doesn't seem to work. Why is that? Does Objective-C check for state equivalency?
I verified your results, and it seems like a bug in iOS. It fails on both the simulator and the device (iOS 6.1). It seems like if the selected and highlighted states are both YES
, then the selected settings should override the highlighted settings. It's mostly implemented that way. The button's text value works like this, but the color seems to get it wrong (defaults to normal).
You might want to try it against iOS 7 if you have the latest XCode to see if they've fixed this, otherwise report it as a bug.
Since selected
isn't a commonly used state for a UIButton
, it probably wasn't properly tested in combination with other states.
As a workaround, in your action
method, you could set the color for the normal state to superDuperSpecialColor
and add another action for the touch up events to set the normal color back to normal. Since the state while the button is pressed should never actually be normal, this won't break anything if they do fix it in the future.