Search code examples
iosuikitios10.2

Can't set navbar title in a VC that's in a tab


In a VC that's embedded in a tabbar I do this:

- (void)viewDidLoad
{
    self.title = LS(@"Choose a soundtrack");
    self.tabBarItem.title = LS(@"Music"); // self.title would not fit into tab 

    [super viewDidLoad];

I expect the self.title to seep through into both navbar and tab and it does end up in tabbaritem (which is why I have to restore it right after).

But it does not show up in the navbar. Similarly, self.navigationItem.title setup has zero effect.

Likewise self.navigationController.title = LS(@"Choose a soundtrack"); has zero effect.

In case that matters the tabbar is pushed into navigation controller like so:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"TabbedStory" bundle:[MediaManager mediaBundle]];
SixTabViewController *stvc = [storyBoard instantiateInitialViewController];
[self.navigationController pushViewController:stvc animated:YES];

Note the excellent name I picked for the tabbar vc. The king is naked.


Solution

  • If your hierarchy is NavController->TabBarController->ViewController then it won't work; view controller's title is displayed in navigation bar of navigation controller if only it's embedded directly. But in your case navigation controller displays tab bar controller and tries do display it's title (which is absent).

    Commonly another hierarchy is used: embed navigation controller into tab bar controller, and then view controller's title will be displayed in navigation controller's navbar.

    If this isn't possible for you, you can change tab bar controller's title every time you switch page in tab bar.