41

I must have overlooked something completely obvious?? but my button displays its image and size correctly, but I simply can't get the Title to show up.

I did a really simple test, the Title does not even show up when I do this:

    CGRect frameBtn = CGRectMake(160.0f, 150.0f, 144.0f, 42.0f);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"left_halfscreen_button.png"] forState:UIControlStateNormal];
    [button setTitle:@"Hello" forState:UIControlStateNormal];
    [button setFrame:frameBtn];
    NSLog(@"Title:%@", [button currentTitle]);
    //prints "Title:Hello
    [self addSubview:button];

I have a factory class that generates custom buttons for me and I thought I messed some detail up there, so I moved the above code directly into my UIView, the title is still blank.

Is this a bug or am I simply missing something right in front of my eyes.

Thank you for the extra set of eyes:)

Venk
  • 5,949
  • 9
  • 41
  • 52
RickiG
  • 11,380
  • 13
  • 81
  • 120

5 Answers5

90

Image overrides title, you need to make the image a background image to show the title.

[button setBackgroundImage:[UIImage imageNamed:@"left_halfscreen_button.png"] 
        forState:UIControlStateNormal];
Tuomas Pelkonen
  • 7,783
  • 2
  • 31
  • 32
  • Thank you Tuomas:) I was pulling my hair out on this one! I thought something like that was going on so I even tried stuff like "bringSubViewToFront" in desperation. I missed the background part in the documentation. Thank you again. – RickiG Mar 24 '10 at 11:46
  • Thanks to Tuomas! I was very puzzled by this and this was the only answer I found. – janineanne Oct 24 '11 at 20:24
29

I just hab spome quite similar problem... Just set the title color, I guess the current one is white ;)

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Stefan Gritzka
  • 301
  • 3
  • 3
13

I found the problem!

It's not Image overriding the title. It is being push off frame by the image.

Try use this:

[btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -img.size.width, 0.0, 0.0 )];

Hope it helps!

JapCon
  • 418
  • 5
  • 10
  • This kind of worked for me. I ended up using [btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0, 0.0, 0.0 )]; Yours pushed the text off to the left of the button. – arsenius Nov 10 '12 at 21:03
  • Hi @arsenius UIEdgeInsetsMake(0, 0, 0, 0) works for you may be because you have a wider frame width for the button. JapCon's -img.size.width may be because he has a different frame width for the button. – George Dec 09 '13 at 11:57
3

When you use UIButton, button uses some built-in size for its frame.

You should set the frame:

button.frame = CGRectMake(0, 0, width, height);

or even better and comfortable:

[button sizeToFit];
Venk
  • 5,949
  • 9
  • 41
  • 52
János
  • 32,867
  • 38
  • 193
  • 353
0

I had a similar problem that the title was not shown. I forgot to set the frame property:

//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
testing
  • 19,681
  • 50
  • 236
  • 417