UIButton only setimage on UIControlStateNormal

ios, uibutton

Solution

Instances of `UIButton` use the "normal" state titles, images, and background images for the other states if no other states are explicitly set, and (annoyingly) even if you explicitly try to set these properties to `nil` for the other states. What I usually do is create a one pixel clear image and use that for the other states, like:

[cardButton setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateSelected]

Another thing you've got to watch out for is that the states `UIControlStateSelected` and `UIControlStateSelected | UIControlStateHighlighted` are actually distinct, so if you don't want your normal state artwork to show up when the user taps and holds the currently selected button, you need to also set:

[cardButton setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateSelected | UIControlStateHighlighted]

Problem

I have a simple UIButton question I need to show an image on a button only if it is in UIControlStateNormal and not show it if I click on it. Here's my code: ``` [cardButton setImage:[UIImage imageNamed:@"cardBack.png"] forState:UIControlStateNormal]; ``` and when a button is clicked, I will change its state: ``` cardButton.selected = card.isFaceUp; ``` Now the problem is, even the button's state is switched to UIControlStateSelected, the image is still there... I have checked the imageForState method and surprisingly found that `[cardButton imageForState:UIControlStateSelected]` returns the same value as `[cardButton imageForState:UIControlStateNormal]` I never set the image value for selected state anywhere and have no idea what happened here. Need your help! Thanks a lot!

Original source