Change title of navbar based on which tab is selec

2019-02-09 06:04发布

I have an iOS app where there is a navigation controller as the root controller but at one part there is a tab bar to select between views in one of the nav bars. It looks similar to the iTunes app (navigation bar on top, tab bar on the bottom). I want the title of my navigation bar to change based on which tab is selected. I have two separate controller files for each tab. Here is what I have tried to use in each so far to fix this to no avail:

self.navigationItem.title = @"Title";
self.navigationController.navigationItem.title = @"title";
[self.navigationController setTitle:@"Live"];    
[self setTitle:@"Top Title"];

How do I change the NavBar title based on which tab is pressed?

6条回答
▲ chillily
2楼-- · 2019-02-09 06:39

You change the title of the bar in the view controller that is currently being displayed.

So for example, in view controller A that you're showing in the tab controller, you might add:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self setTitle:@"A"];
    self.tabBarController.navigationItem.title = @"A";
}

Same goes for B, C, etc.

查看更多
We Are One
3楼-- · 2019-02-09 06:39

Just Two Lines of Code..Only thing is, you need to use viewWillAppear method

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.navigationItem.title = @"Your Title";
}

PS: Inspired from Above Answers...

查看更多
姐就是有狂的资本
4楼-- · 2019-02-09 06:42

In your ViewControllers that are in the tabs:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.title = self.title;
}
查看更多
神经病院院长
5楼-- · 2019-02-09 06:42

You can subclass the UITabBarController, set the delegate to itself, and use the delegate to set its title when the view controller is selected:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //change the title based on viewController that is selected
    self.title = @"New title";
}
查看更多
疯言疯语
6楼-- · 2019-02-09 06:42
 UITabBarController *tabController = (UITabBarController *)self.parentViewController;

 tabController.navigationItem.title = @"ABC";

This is working for me

From some R&D on internet

You have to pass the navigationItem up the chain. The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController. The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem

So to recap:

UINavigationController title = topViewController.navigationItem.title

UITabBarController tabTitle = selectedViewController.navigationItem.title

UIViewController title = navigationItem.title

查看更多
Juvenile、少年°
7楼-- · 2019-02-09 07:01

If the individual view controllers presented by the tab bar controller have their own navigation bars, then

[self setTitle:@"Foo"];

will set both the tab bar label, as well as the navigation bar title.

If the navigation controller is at the top level (i.e. the tab bar is inside the navigation controller), then you might have to set the navigation bar title's manually (and you'll want to do this in viewDidAppear rather than viewDidLoad, because these child controllers are not reloaded every time you switch), e.g.:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.navigationController.navigationBar.topItem setTitle:@"Foo"];
}

Alternatively, you could do this navigation bar title adjustment in your UITabBarControllerDelegate method didSelectViewController.

If this doesn't do it, you might have to clarify your question, describing the hierarchy of controllers (e.g. is the tab bar controller inside navigation bar, or vice versa).

查看更多
登录 后发表回答