简体   繁体   中英

How do I stop a UIButton's imageView from changing when pressed?

I have a method which sets a storyboard-placed UIButton's imageView property. After its set, it looks fine. However, when the button is tapped, its highlight state changes imageView's image property back to the old image. How do I stop this from happening? Here is my method:

- (void)setThumbButtonPhoto:(UIImage *)image
{
   // profilePhoto is an IBOutlet property of class UIButton pointing to the 
   // UIButton on my storyboard.

   // Button image is changed correctly here
    profilePhoto.imageView.image = image;

    // But then mysteriously changed back to the old image when tapped.
    // The following commented out lines I have all tried (one at a time of course)
    // and none have solved my problem -->

    // [profilePhoto.imageView setHighlightedImage:nil];
    // profilePhoto.imageView.highlightedImage = image;
    // profilePhoto.adjustsImageWhenHighlighted = NO;
    // [profilePhoto setBackgroundImage:image forState:UIControlStateHighlighted];
    // [profilePhoto setImage:image forState:UIControlStateNormal];
    // [profilePhoto setImage:image forState:UIControlStateHighlighted];
    // [profilePhoto setImage:image forState:UIControlStateSelected];
}

You should use this method to set the image properly on your button: - (void)setImage:(UIImage *)image forState:(UIControlState)state

So:

[profilePhoto setImage:image forState:UIControlStateNormal];
[profilePhoto setImage:image forState:UIControlStateHighlighted];

use an UIImageView to hold the image

and put a Custom mode UIButton, no text, no image, no background ; under it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM