3

I have a set of UIButtons mapped out in IB. I want to randomly add images to a few buttons for each round of an education game.

You can see some images stashed at the top of the page:

enter image description here

As you can also see, the images in the buttons are blue. So, what am I doing wrong (i.e. why are they blue?)? Are they being set as background images?

I'm adding the images as follows:

    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 indexes that correspond to 5 button tags.

Thanks very much for any ideas.

What I'm seeing when I look for UIButton in Xcode help:

enter image description here

UPDATE: With help from folks 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];
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • Please read the document called "Buttons" in the Xcode documentation. It explains this. – rdelmar Oct 22 '14 at 19:35
  • I've marked up my question with what I'm seeing with the Xcode docs.. i.e.. not much. could you please provide a URL, or instructions for idiots :) – ICL1901 Oct 22 '14 at 19:40
  • 1
    If you search "buttons" in the documentation, you should see something called, "Appearance of Buttons" in the SDK guides at the bottom of the list. Look at that. – rdelmar Oct 22 '14 at 19:41
  • 1
    You'd be blue too, if you were cooped up in that tiny little phone. – Hot Licks Oct 22 '14 at 19:57
  • yep. I'm definitely a plus size. – ICL1901 Oct 22 '14 at 20:11

2 Answers2

5

I quote:

Using the Image field, you can specify an image to appear within the content of your button. ... Note that this image will be automatically rendered as a template image within the button, unless you explicitly set its rendering mode to UIImageRenderingModeAlwaysOriginal.

So, probably:

UIImage *image = [[UIImage imageNamed:temp5] 
                   imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tmpButton4 setImage:image ...

(etc)

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • great. Thanks.. could you please point me to the documentation you quote from? – ICL1901 Oct 22 '14 at 19:42
  • Oh, the link is hidden underneath the word 'quote' — it's this: https://developer.apple.com/library/iOS/documentation/userexperience/conceptual/UIKitUICatalog/UIButton.html (scroll down to the heading 'Images') – Tommy Oct 22 '14 at 19:44
  • Thanks Tommy, between your answer and the docs -- thanks too @rdelmar -- I got it.. I'm posting the result in my question.. – ICL1901 Oct 22 '14 at 20:13
0

More elegant option, in my opinion, could be to go to UIButton's property inspector in interface builder and select Custom under Type. The blue version that you are referring to only appears if you keep the default System type for your buttons. Than you can initialize the buttons as usual, either by code or inside the interface builder.

Despotovic
  • 1,807
  • 2
  • 20
  • 24