UIButton Background Image Does Not Change

I want to change the background image of the button according to the response of the web service. if in response I get unread messages in true, then the image will be different, and if there are no unread messages, the image will be different. I get the answer very well, but I can’t change the background image of the button.

This is the fragment that I use

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

The solution can be very simple if you look at it. Store the global UIButton, not the local one. This helps, because when the web request returns a response, the button must be present in the scope and not be deleted from memory.

Therefore use

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

OR

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

And, if you check the button click, follow these steps:

 -(IBAction)checkForMsgCount: (id)sender { UIButton *buttonObj = (UIButton*)sender; [buttonObj setBackgroundImage:[UIImage imageNamed:@"unread.png"] forState:UIControlStateNormal]; } 

This is a background image change for a button that responds to a function.

+7
source
 [YourBtn setImage:[UIImage imageNamed:@"your.png"] forState:UIControlStateNormal]; 

please try.

You can also try this to check it out, do you really have these photos in your package?

  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"); } 
+1
source

try the code below

 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]; } 
+1
source

First check the IBOutlet connection on your button :)

And set your code after receiving data from the web service, i.e. after getting unread value

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

You add a button image for UIControlStateNormal or UIControlStateSelected

try

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

find if the image exists and is correctly added to the kit, and the name is correct.

0
source

Set the background images for UIControlStateNormal and UIControlStateSelected as separate lines .

 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]; } 
0
source
 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]; 

I hope this works for u ....

0
source

after setting the background image

 [badgeTitle setNeedsLayout]; 
0
source

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


All Articles