In iOS7.1 UIButton, the installed image does not work?

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

This code works fine in iOS7, but in iOS7.1 it does not change the image of UIButton. I also tried to do this using setImage, but did not work.

thank

+4
source share
2 answers

Just in case, if others have the same problem, and you need another hint on how to fix it:

I also had this problem and finally it turned out that setting the button (background) image does not work on iOS 7.1 if the button is disabled.

, , . setNeedsLayout , . , UIButton, , :

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

(16497031).

+9

iOS 7.1,

UIButton .

-(void)setImage:(UIImage *)image forButtonState:(UIControlState)state
{
  CGFloat version = [[UIDevice currentDevice] systemVersion].floatValue ;
  if( version  <= 7.1)
  {
      BOOL isFirstTime = YES ;
      for(UIView *aView in self.subviews)
      {
          if([aView isKindOfClass:[UIImageView class]])
          {
              [(UIImageView *)aView setImage:image] ;
              isFirstTime = NO ;
              break ;
          }
      }
      if(isFirstTime)
      {
          CGRect frame = self.frame ;
          frame.size = image.size ;
          UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
          [imageView setImage:image];
          [self addSubview:imageView];

      }

  }
  else

    [self setImage:image forState:state];

}

.

:)

0

Source: https://habr.com/ru/post/1532250/


All Articles