UIButton: set image for selected-highlighted state

user478681 picture user478681 · Apr 8, 2011 · Viewed 123.5k times · Source

I set an images for button's states Normal,Highlighted and Selected, but when the button in selected state and I press/highlight it I didn't see my highlighted image but just grayed picture. Is it possible to set an image for highlighted state when the button selected?

my code:

[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

when I do:

[button setSelected:YES];

and press the button, the "pressed.png" image doesn't select.

Answer

user478681 picture user478681 · Aug 18, 2011

I found the solution: need to add addition line

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];