Search code examples
iosobjective-cuinavigationcontrolleruitabbar

How to hide tab bar in my custom navigation controller?


I want to hide bottom tab bar when pushing new view controllers into UINavigationController (I'm using default tab bar). I do this by inherit UINavigationController and create my custom navigation controller.

I override below two functions:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    [super pushViewController:viewController animated:YES];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    NSLog(@"NavigationController: View controller count = %lu", self.viewControllers.count);
    if (self.viewControllers.count <= 2) {
        self.tabBarController.tabBar.hidden = NO;
    }
    return [super popViewControllerAnimated:animated];
}

In app delegate:

// Use my custom navigation controller
NavigationController *myNav1 = [[NavigationController alloc] initWithRootViewController:myView];
myNav1.navigationBar.translucent = NO;
myNav1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"abc" image:[UIImage imageNamed:@"abc"] selectedImage:[UIImage imageNamed:@"abc"]];

...

self.tabController.viewControllers = [NSArray arrayWithObjects:myNav1, myNav2, myNav3, myNav4, nil];

Problem:

  1. When pushing new view controllers, the bottom bar will hides immediately, I want it to look like be pushed with view controllers.
  2. When I try to return to previous view controller use gestures, the bottom bar will show, even I stop returning, it will not hide.

I know a flag hidesBottomBarWhenPushed. If I use that flag, I need to set it to YES every time I need to push a view controller. It is a little complex.

Is there a way to fix these problems in my custom navigation controller?


Solution

  • Seems solves my problem (code is not very good, will do some update later):

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        NSLog(@"NavigationController.pushViewController: view controller count %lu", self.viewControllers.count);
    
        UIViewController *lastVC = nil;
        if (self.viewControllers.count > 0) {
            lastVC = self.viewControllers[self.viewControllers.count - 1];
        }
        if (lastVC != nil) {
            lastVC.hidesBottomBarWhenPushed = YES;
        }
        [super pushViewController:viewController animated:YES];
        if (self.viewControllers.count == 2) {
            lastVC.hidesBottomBarWhenPushed = NO;
        }
    }