SteBra
SteBra

Reputation: 4247

How to check which image is on a button's background

I need to check which image is set as a button background. There can be three options, which program sets based on several factors. If the specific background is set after some animation, certain functions should be triggered.

So, i need something like: if(button.getBackgroundImage == certainBackgroundImage){ do something}

Now, I am aware that there is a method to set button background to some image:

[button setBackgroundImage:[UIImage imageNamed:@"cloudRed.png"] forState:UIControlStateNormal];

is there a method that will do something like this:

UIImage *red cloud = = [UIImage imageNamed:@"cloudRed.png"];
if([cloud imageForState:UIControlStateNormal] == _cloudRed ){

                                 NSLog(@"OMG");
                             }

Upvotes: 0

Views: 3484

Answers (1)

gskspurs
gskspurs

Reputation: 186

The backgroundImageForState method of UIButton returns the UIImage

- (UIImage *)backgroundImageForState:(UIControlState)state

So pretty much how you described:

UIImage *imageToCheckFor = [UIImage imageNamed:@"someImage.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:imageToCheckFor forState:UIControlStateNormal];

// Later to Check

if ([button backgroundImageForState:UIControlStateNormal] == imageToCheckFor) {

}

Upvotes: 3

Related Questions