简体   繁体   中英

IOS 8 Button background image checking not working

Button background image checking is failing in IOS 8, it is working proper in IOS7 and earlier.

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


}
}

Try comparing the UIImage returned by the backgroundImageForState: instead of the UIButton imageView property.

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'
}

A best way to check if a button is selected or not, is to used your button's state, like UIButtonStateNormal , or UIButtonStateSelected . You can just change its selection like that:

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

In addition, set the right image following your button's state in your xib or in code if you create it programatically:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM