Change background image of a button without extra buttons
I've set a button with an image as it's background, and I'm trying to get the image to change back and forth from 2 different images when pressed. For example, a happy face when pressed would switch to a sad face, 开发者_StackOverflowand when pressed again would switch back to a happy face and so on.
Any help you can offer would be amazing before I pull anymore hair out.
set the images for two different states: normal and selected, have the button target an action that toggles the selected state on the button/sender
UIButton* myButton;
- (IBAction)buttonPressed {
if(alreadySelected) {
myButton.imageView.image = myNormalImage;
} else {
myButton.imageView.image = mySelectedImage;
}
}
Not sure, but I think this should do it.
What BoopMeister said, except as bshirley pointed out, you need this API call:
[myButton setImage:happyImage forState:UIControlStateNormal];
精彩评论