IOS 8按钮背景图像检查不工作

时间:2014-11-04 11:45:29

标签: ios iphone image uibutton ios8

按钮背景图像检查在IOS 8中失败,它在IOS7及更早版本中正常工作。

-(void)buttonAction:(UIButon*)tempbutton
{
//working in IOS 7 but broken in IOS 8
if(tempbutton.currentBackgroundImage ==[UIImage imageNamed:@"uncheck.png"]){


}
}

1 个答案:

答案 0 :(得分:4)

尝试比较UIImage而不是backgroundImageForState: imageView属性返回的UIButton

if ([[tempbutton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed@"uncheck.png"]]){
  // Button has a background image named 'uncheck.png'
} else{
  // Button has not a background image named 'uncheck.png'
}

检查是否选择了按钮的最佳方法是使用按钮的状态,例如UIButtonStateNormalUIButtonStateSelected。你可以改变它的选择:

[tempbutton setSelected:![self.btnMenu isSelected]];

此外,如果以编程方式创建,请在xib或代码中按照按钮状态设置正确的图像:

[self.tempbutton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
[self.tempbutton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];