uinavigationcontrollerapple-tv

UINavigationBar does not show on second ViewController on Apple TV


I have the following code in AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController* viewController = [sb instantiateViewControllerWithIdentifier:@"ViewController"];
    
    UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    
    UILabel* topBar = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    topBar.text = @"Custom Navigation";
    
    [navigationController.navigationBar.topItem setTitleView:topBar];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setRootViewController:navigationController];
    [self.window makeKeyAndVisible];
    return YES;
}

My screen has a unbutton and appear like

When the button is clicked, the following code is run;

UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController* second = [sb instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController setNavigationBarHidden:NO];
[self.navigationController pushViewController:second animated:YES];

The navigation bar is always hidden on the second screen. I tried [self.navigationController setNavigationBarHidden:NO] and [self.navigationController.navigationBar setHidden:NO] but it does not work.

My code runs successfully on iPhone or iPad, but does not work on Apple TV. What is the solution to show the navigation bar on the second screen?


Solution

  • Don't use UINavigationBar on Apple TV. UITabBar is more useful. If you make custom tab bar, implement UITabBarController to your interface and write fallowing code to the viewDidLoad method.

    customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomTopBar" owner:self        options:nil] objectAtIndex:0];
    [self.view addSubview: customView];