ios - 为什么按钮图像会在用户点击后还原?

标签 ios objective-c uibutton uiimage

我使用这段代码更改了 viewWillAppear 中的按钮图像

UIImage *buttonImage = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:@"button_preference"]];

Button.imageView.image =buttonImage;

但是当它被点击时,它会恢复到 IB 中设置的图像。所以我尝试添加

[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

那么图像一开始就没有改变?

最佳答案

首先

Button.imageView.image =buttonImage;

[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

两者不同,因为第一个将图像设置为按钮的 imageView,第二个将图像设置为按钮的背景,所以我不确定,但问题应该是 button.imageView 图像的叠加或者您的第二个代码没有图像 (我的意思是 NULL)

我的意见是像这样设置按钮图片

[leftButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

这对你更好。

关于ios - 为什么按钮图像会在用户点击后还原?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18755329/

相关文章:

ios - 在相机 View 上显示 UILabel

ios - IndexPathForCell 和 IndexPathForRowAtPoint 崩溃

objective-c - Cocoa窗口位置异常

ios - PhoneGap 2.9.0 不工作构建错误

ios - 基于另一个 UIPickerView 显示 UIPickerView

ios - 如何将UIButton的名称保存在字符串中?

ios - 使用 NSPredicate 查找用户组

ios - 为什么它不能在 swift 2 的 Playground 上显示任何东西?

ios - 更改 UIButton attributedStrings enumerateSubstrings 字符串范围的标题

iphone - UIButton的addtarget : not called after touch it!