简体   繁体   English

如何在按下时停止更改UIButton的imageView?

[英]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. 我有一个方法来设置故事板放置的UIButton的imageView属性。 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. 但是,当点击按钮时,其突出显示状态会将imageView的图像属性更改回旧图像。 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 您应该使用此方法在按钮上正确设置图像: - (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 使用UIImageView来保存图像

and put a Custom mode UIButton, no text, no image, no background ; 并放置自定义模式UIButton,没有文字,没有图像,没有背景; under it 在它下面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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