Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the title of the UINavigationBar

Is it possible to set self.navigationItem.title = @"MyTitle";after the view has loaded ?

I am using a search bar and in the same view I feed a UITableView using the search results. So I want to set the search bar's text value as the title of the navigation bar, after the search results loaded.

I am using iOS 4.3.

like image 202
chinthakad Avatar asked Mar 23 '12 14:03

chinthakad


3 Answers

Just set the title of UINavigationBar like this:

navBar.topItem.title = @"sample string";
like image 197
SamirChen Avatar answered Sep 21 '22 05:09

SamirChen


Found a solution. I followed Pedro Valentini's post.

This is what I did,

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = UITextAlignmentCenter;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release]; 
like image 21
chinthakad Avatar answered Sep 21 '22 05:09

chinthakad


Normally the titleView will be an UILabel, so you can fetch it, cast it, and set its text.

For some reason I had problems using this in viewDidLoad, adding it to viewWillAppear seemed to do the trick.

((UILabel *)self.navigationController.navigationBar.topItem.titleView).text = @"your title here"
like image 33
Kevin R Avatar answered Sep 24 '22 05:09

Kevin R