【问题标题】:UIButton with image losing its newly set image带有图像的 UIButton 丢失其新设置的图像
【发布时间】:2009-10-02 22:00:13
【问题描述】:

我在 IB 中设置了一个 UIButton(自定义,带图像),这是我的代码中的一个 IBOutlet。

在 viewController 中的 viewDidLoad 方法中,我试图更改 UIButton 的现有图像

UIImage *newbuttonimage = [UIImage imageNamed:@"newbuttonimage.png"];
testbutton.imageView.image = newbuttonimage;

好的,这在启动应用程序时有效,但是每当您与按钮交互(按下它)时,它都会更改为原始图像(在 IB 中设置)。整个项目中没有其他代码可以更改图片,这是怎么回事?

顺便说一句,当我将上述代码放在项目的任何其他部分时,它不会更改图像。

【问题讨论】:

    标签: iphone uiimageview uiimage uibutton


    【解决方案1】:

    您应该尝试使用setImage:forState 而不是直接分配图像; UIButton 有多种状态,如果设置不当,可能会产生不需要的行为(类似于您所看到的行为)。

    【讨论】:

      【解决方案2】:

      哇!我应该改用这段代码:

      UIImage *newbuttonimage = [UIImage imageNamed:@"newbuttonimage.png"];
      [testbutton setImage:newbuttonimage forState:UIControlStateNormal];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-02
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多