开发者

Change background image of a button without extra buttons

开发者 https://www.devze.com 2023-03-15 04:34 出处:网络
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.

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消