2

I am trying to use a UILabel to replace the title at the UINavigationBar, the code is as follows:

UINavigationBar *bar = [self.navigationController navigationBar];
    [bar setBackgroundColor:[UIColor blackColor]];

    UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
    nav_title.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
    nav_title.textColor = [UIColor whiteColor];
    nav_title.adjustsFontSizeToFitWidth = YES;
    nav_title.text = title;
    nav_title.backgroundColor = [UIColor clearColor];
    [bar addSubview:nav_title];
    [nav_title release];

The problem is that, how do I remove the original title of the bar? I didn't declare any self.title = @"title", but it always shows it there:

enter image description here

If I do self.title = nil, then everything is gone... How do eliminate this mysterious title from the navbar and just use the UILabel I created.

adit
  • 32,574
  • 72
  • 229
  • 373

3 Answers3

4

Why don't you just do self.title = @""?

EDIT: Try this?

UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];

UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
self.title = @"";
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52
shabbirv
  • 8,958
  • 4
  • 33
  • 34
  • that is because you set nav_title.text = title, and then set title = @"" – Drew C May 08 '11 at 19:31
  • so what I did is add the UILabel as a subview then do self.title = @"" and then inside viewWillDisapper I remove the label from the superView.. Is there any beter way than this? – adit May 08 '11 at 20:24
  • @shabzco, is 220x25 is the default label size for navigation bar ? – Zubair Mar 04 '14 at 09:54
2

Use self.navigationItem.titleView = nav_title; instead of adding your label as a subview.

Drew C
  • 6,408
  • 3
  • 39
  • 50
  • the issue is that can you can't adjustsFontSizeToFitWidth with that. Initially I was using that, but I was disturbed by the .... when the text is long – adit May 08 '11 at 17:25
  • Maybe wrap the label in an ordinary UIView, let the outer view do the autoresizing and have the inner label resize its font. – Evadne Wu May 08 '11 at 18:13
1

Use this:

  UILabel *label = [[UILabel alloc]init];
  [label setBackgroundColor:[UIColor clearColor]];
  [label setTextColor:[UIColor whiteColor]];
  [label setText:self.title];
  label.adjustsFontSizeToFitWidth=YES;
  label.lineBreakMode=UILineBreakModeWordWrap;
  label.numberOfLines=0;
  [label setFont:[UIFont boldSystemFontOfSize:16.0]];
  [self.navigationController.navigationBar.topItem setTitleView:label];
  [label release];

Hope this will help u..!
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90