Skip to main content

UIButton of type UIButtonTypeCustom will not display Title (iPhone)



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:)


Comments

  1. 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];

    ReplyDelete
  2. 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];

    ReplyDelete
  3. 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!

    ReplyDelete
  4. 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);

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.