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

时间:2012-06-02 16:19:15

标签: objective-c ios xcode button uistoryboard

我有一个设置故事板放置的UIButton的imageView属性的方法。设定后,它看起来很好。但是,当点击按钮时,其突出显示状态会将imageView的图像属性更改回旧图像。我如何阻止这种情况发生?这是我的方法:

- (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];
}

2 个答案:

答案 0 :(得分:2)

您应该使用此方法在按钮上正确设置图像: - (void)setImage:(UIImage *)image forState:(UIControlState)state

所以:

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

答案 1 :(得分:0)

使用UIImageView来保存图像

并放置自定义模式UIButton,没有文字,没有图像,没有背景;在它之下

相关问题