Objective C. What's wrong with this UIButton image?
I have a set of UIButtons displayed in IB. I want to randomly add images to several buttons for each round of the educational game.
You can see some of the images tagged at the top of the page:
As you can also see, the images on the buttons are blue. So what am I doing wrong (that is, why are they blue?)? Are they set as a background image?
I am adding images like this:
int tmpTag0 = [[currentButtonArray objectAtIndex:0] integerValue];
UIButton *tmpButton0 = (UIButton *)[self.view viewWithTag:tmpTag0];
[tmpButton0 setImage:[UIImage imageNamed:temp1] forState:UIControlStateNormal];
........
int tmpTag4 = [[currentButtonArray objectAtIndex:4] integerValue];
UIButton *tmpButton4 = (UIButton *)[self.view viewWithTag:tmpTag4];
[tmpButton4 setImage:[UIImage imageNamed:temp5] forState:UIControlStateNormal];
currentButtonArray is an array of 5 random button indices that correspond to 5 button tags.
Thanks a lot for any ideas.
What I see when I search for UIButton in Xcode help:
UPDATE: With help from the people below, this worked for me:
int tmpTag0 = [[currentButtonArray objectAtIndex:0] integerValue];
UIButton *tmpButton0 = (UIButton *)[self.view viewWithTag:tmpTag0];
UIImage *buttonImage0 = [UIImage imageNamed:temp1];
buttonImage0 = [buttonImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tmpButton0 setImage:buttonImage0 forState:UIControlStateNormal];
source to share
I quote :
Using the Image field, you can specify an image to be displayed inside the content of your button .... Note that this image will be automatically rendered as a template image inside the button unless you explicitly set the render mode to UIImageRenderingModeAlwaysOriginal.
So maybe:
UIImage *image = [[UIImage imageNamed:temp5]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tmpButton4 setImage:image ...
(etc.)
source to share
A more elegant option, in my opinion, might be to go to the UIButton
property inspector in the interface builder and select Custom
pod Type
. The blue version in question only appears if the System
default type is used for your buttons . Then you can initialize the buttons as usual, either through code or inside the interface constructor.
source to share