简体   繁体   中英

SetTint of UIButton won't hold

I'm loading the image of UIButton via web as:

NSURL *imageURL = [NSURL URLWithString:[ruleImageArr objectAtIndex:0]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[self.classicBackgroundButton setImage:image forState:(UIControlStateNormal)];

Which is working fine. So when user taps on it I want the button to setTint . Here is how I'm doing it:

- (IBAction)classicBackgroundButtonPressed:(id)sender {
    [self.classicBackgroundButton setTintColor:[UIColor greenColor]];
}

But it's not working. Can't seems to figure out whee the problem is? I've searched it around on stackOverFlow but its mostly how I can replace the UIButton image on tap. These images are loading from web and selecting them should highlight them to give a feel that this button is selected. Any Help would be apperciated.

UiImage must have template rendering mode in order to work with tint. Something like this:

UIImage *imageTint = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:imageTint forState:UIControlStateNormal]; 

you could use another UIControlState to manage this.

NSURL *imageURL = [NSURL URLWithString:[ruleImageArr objectAtIndex:0]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
[self.classicBackgroundButton setImage:image forState:(UIControlStateSelected)];
[self.classicBackgroundButton setImage:image forState:(UIControlStateHighlighted)];
[self.classicBackgroundButton setImage:image forState:(UIControlStateNormal)];

Use this instead

- (IBAction)classicBackgroundButtonPressed:(UIButton *)sender {

    [sender setTintColor:[UIColor greenColor]];
}

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