【问题标题】:UIButton background image not changingUIButton背景图像没有改变
【发布时间】:2013-07-26 10:22:36
【问题描述】:

我想根据 Web 服务响应更改按钮背景图像。如果作为回应,我将未读消息设为 true,则图像将不同,如果没有未读消息,图像将不同。我得到了很好的响应,但无法更改按钮的背景图片。

这是我正在使用的 sn-p

if(unread>0){
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}
else{
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
}

【问题讨论】:

  • 您的按钮应该是“自定义按钮”。
  • 您是否尝试过调试以查看未读假定的值?也许问题在于它没有您期望的价值。更多的上下文,显示更多的源代码会有所帮助。
  • @RamyAlZuhouri 我已经对其进行了调试,它处于正确的状态,但无法更改图像。不知道为什么
  • 你在xib中设置图片了吗?删除它们或在 xib 中仅设置背景图像,如果您这样做了,请再试一次。
  • @iphonemaclover 是的,它加载一次,我只是说它是为了检查代码是否工作,因为如果图像要更改一次,那么使用按钮操作中的 if 和 else 语句再次更改它很容易,或者无论你想去哪里,你都知道。

标签: iphone ios objective-c uibutton


【解决方案1】:

如果您看一下,解决方案可能非常简单。 保留一个全局 UIButton 而不是本地的。这很有帮助,因为当 Web 请求返回响应时,按钮需要存在于范围内,而不是从内存中删除。

所以用,

self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];

[self.myButton setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];

或

[self.myButton setImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];

而且,如果您检查按钮的点击情况,请按照以下步骤操作:

-(IBAction)checkForMsgCount: (id)sender
{
  UIButton *buttonObj = (UIButton*)sender;

[buttonObj setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
}

这会更改响应功能的按钮的背景图像。

【讨论】:

  • 这没有任何意义,如果按钮被删除,我调试了很多时间,但它一直都在那里。
  • 该解决方案明确指出,您需要清楚,什么按钮响应该方法。这可以通过将发送者对象类型转换为 UIButton,然后将图像更改为它来使用。试试这个并告诉它是否不起作用
  • 我自己用过这个。告诉你,它会起作用的。试试吧!
【解决方案2】:

请尝试下面的代码

if(unread>0){
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState :UIControlStateSelected];
}
else{
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState :UIControlStateSelected];
}

【讨论】:

  • 他想为 UIControlState selected 和 normal 更改它。您应该调用该方法两次来为这两种控制状态添加图像。此外,您应该对这段代码以及用户做错了什么给出一些解释。
  • 是的,它会起作用,请检查你是否只想改变 UIControlStateNormal 它会起作用
  • 为什么不把forstate放在第二行?
  • 哦,只是语法错误,请在应用此代码时添加此代码
【解决方案3】:
[YourBtn setImage:[UIImage imageNamed:@"your.png"] forState:UIControlStateNormal];

请尝试一下。

如果你的包里真的有这张照片,你也可以试试看吗?

        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *imagePath = [[NSString alloc] initWithFormat:@"%s/yourButton.png", [bundlePath  UTF8String]];


if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
    NSLog(@"image exists");
}
else{
    NSlog(@"image Does Not Exist");
      }

【讨论】:

    【解决方案4】:
    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal|UIControlStateSelected];
    

    您是在为UIControlStateNormal 或UIControlStateSelected 添加按钮图像

    试试

    [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
    

    查找图像是否存在并正确添加到包中以及名称是否正确

    【讨论】:

    • 图像存在@Lithu
    • @BaZinga 尝试在控制状态正常和选中的两行中设置图像
    • @BaZinga,因为您正在设置敌人被选中,请尝试为未选中状态设置图像
    【解决方案5】:

    将 UIControlStateNormal 和 UIControlStateSelected 的背景图像设置为单独的行。

    if(unread>0){
    
       [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateNormal];
    
       [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] UIControlStateSelected];
    } else {
    
       [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateNormal];
    
       [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] UIControlStateSelected];
    }
    

    【讨论】:

    • 正确,但请至少添加一些解释。
    • 与上面发布的答案相同!
    【解决方案6】:

    首先检查按钮的 IBOutlet 连接:)

    并在从 Web 服务获取数据后设置您的代码,即在获得 unread 的值之后

    if(unread>0){
        [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal];
    }
    else{
        [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal];
    }
    

    【讨论】:

    • 它与 UIControlStateNormal 处于“或”中。顺便说一句,只有 UIControlStateNormal 它不起作用
    • 检查按钮的 IBOutlet 连接 :)
    • 然后从开始做一个新按钮:)
    • 老兄,这将有助于删除以前的对象并制作新的
    【解决方案7】:
    UIButton *btn = [[UIButton alloc]init];
        btn.frame = CGRectMake(50, 50, 100, 50);
        [btn setTitle:@"ok" forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal];
        btn.backgroundColor=[UIColor blueColor];
    

    我希望这对你有用....

    【讨论】:

    • 试试这个,if(unread.text.length>0){ [badgeTitle setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; } else{ [badgeTitle setBackgroundImage:[UIImage imageNamed:@"read.png"] forState:UIControlStateNormal]; } 如果未读是字符串,那么这是可行的......
    【解决方案8】:

    设置背景图片后

    [badgeTitle setNeedsLayout];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多