UINavigationBar Name

I am trying to use UILabel to replace the title in a 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 to remove the original title bar? I did not declare a single self.title = @ "title", but it always shows it there:

enter image description here

If I do self.title = nil then everything will disappear ... How to remove this mysterious title from the navigation bar and just use the UILabel I created.

+2
source share
3 answers

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

EDIT: Try it?

 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]; 
+4
source

Use self.navigationItem.titleView = nav_title; instead of adding tags as a preview.

+2
source

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..! 
+1
source

All Articles