objective-c - 如何阻止 UIButton 的 imageView 在按下时发生变化?

标签 objective-c ios xcode button uistoryboard

我有一个方法可以设置 Storyboard放置的 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];
}

最佳答案

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

所以:

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

关于objective-c - 如何阻止 UIButton 的 imageView 在按下时发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863973/

相关文章:

ios - Objective C Completion Block 导致 swift 崩溃

iOS 布局 : push UIView below automatically

ios - 拍摄隐藏的 UIView 的快照

ios - 使用适用于 iOS 的 Obj C 启动合并调用

iphone - 移除行为异常的 objectAtIndex

Xcode/GCC 链接问题 : file is not of required architecture

ios - iOS-广播SMS(将SMS发送给多个收件人,而不是作为一组MMS?)

objective-c - 使用 float 表示货币值的结果不正确

iphone - 通知中心 - 我如何知道是否有 'unread' 通知?

ios - 奇怪的构建错误,类名问题?