Set UINavigationBar Header

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

I use a search bar, and in the same view, I submit a UITableView using the search results. Therefore, I want the text value in the search bar to appear as the title of the navigation bar after loading the search results.

I am using iOS 4.3.

+6
source share
7 answers

Found a solution. I followed Pedro Valentini .

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

Just set the UINavigationBar header as follows:

 navBar.topItem.title = @"sample string"; 
+15
source

Usually the titleView will be a UILabel, so you can get it, drop it and set its text.

For some reason, I had problems using this in viewDidLoad , adding it to viewWillAppear seems like a trick.

 ((UILabel *)self.navigationController.navigationBar.topItem.titleView).text = @"your title here" 
+2
source

Yes. You can dynamically set the title of the navigationItem element during any part of the UIViewController life cycle. The set operation is handled by the getter method, which sends a message to the UINavigationController to load the navigation view contents to reflect the new name.

+1
source

This should work if self is inside "inside" of a UINavigationController .

0
source
 _navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; _navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; UINavigationItem *navItem = [UINavigationItem alloc]; navItem.title = @"Title"; 

Try it.

0
source
  self.navigationItem.title = @"Terms and Conditions"; 
0
source

Source: https://habr.com/ru/post/911501/


All Articles