Iphone UINavigationBar标题

Iphone UINavigationBar标题,iphone,objective-c,uinavigationbar,Iphone,Objective C,Uinavigationbar,我正在尝试使用UILabel替换UINavigationBar上的标题,代码如下: UINavigationBar *bar = [self.navigationController navigationBar]; [bar setBackgroundColor:[UIColor blackColor]]; UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];

我正在尝试使用UILabel替换UINavigationBar上的标题,代码如下:

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];
问题是,如何删除酒吧的原始标题?我没有声明任何self.title=@“title”,但它总是显示在那里:


如果我做self.title=nil,那么一切都消失了。。。如何从导航栏中删除这个神秘的标题,而只使用我创建的UILabel。

使用
self.navigationItem.titleView=nav_title而不是将标签添加为子视图。

为什么不直接执行self.title=@

编辑:试试这个

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

使用以下命令:

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

问题是你不能用它来调整ntSizeToFitWidth。起初我是用这个,但我被。。。。当文本较长时,可以在普通UIView中包装标签,让外部视图进行自动调整大小,并让内部标签调整字体大小。这是因为您设置了nav_title.text=title,然后设置了title=@“”,所以我所做的是将UILabel添加为子视图,然后执行self.title=@“”然后,inside ViewWillopser我将从superView中删除标签。。还有比这更好的方法吗?@shabzco,导航栏的默认标签尺寸是220x25吗?