Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In iOS7.1 UIButton set image not working?

[_firstPrincipleButton setBackgroundImage:[UIImage imageNamed:@"i_press.png"]
                                 forState:UIControlStateNormal];

The above code is working fine in iOS7 but in iOS7.1 this is not changing the image of UIButton. I have also tried to do this with setImage but didn't work.

Thanks

like image 377
user2893180 Avatar asked Jan 11 '23 23:01

user2893180


1 Answers

Just in case that others have the same problem and need another hint on how to fix this:

I also had this problem and finally found out that setting a button's (background) image doesn't work on iOS 7.1, if the button is disabled.

Not sure if this fixes your problem, but it was mine. Calling setNeedsLayout didn't help in my case, unfortunately. What you can do as a workaround is either to override UIButton or to add a category that contains a method like the following to set an image:

- (void)setImage:(UIImage *)img forButtonState:(UIControlState)state
{
    BOOL shouldEnable = self.enabled;
    self.enabled = YES;
    [self setImage:img forState:state];
    self.enabled = shouldEnable;
}

Filed a bug report for this issue (16497031).

like image 81
Flo Avatar answered Jan 19 '23 17:01

Flo