Pro Tip: Bitwise OR UIControlState to set image for different states on a UIButton

There is a case where you want a different highlight image for a button when it is selected and highlighted instead versus not selected and highlighted. For example a button that is able to be "on" or "off" with a glowing light or something. I figured out today that you can | UIControlStates together to specify those images (or titles).

[myButton setImage:offImage forState:UIControlStateNormal];
[myButton setImage:offHighlightedImage forState:UIControlStateHighlighted];
[myButton setImage:onImage forState:UIControlStateSelected];
[myButton setImage:onHighlightedImage forState:UIControlStateSelected|UIControlStateHighlighted];

Comments