如何在我的自定义导航控制器中隐藏标签栏?

时间:2017-02-18 06:30:27

标签: ios objective-c uinavigationcontroller uitabbar

我想在将新视图控制器推入UINavigationController时隐藏底部标签栏(我正在使用默认标签栏)。我这样做是通过继承UINavigationController并创建我的自定义导航控制器。

我覆盖了以下两个函数:

- (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];
}

在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];

问题:

  1. 当推动新的视图控制器时,底部栏会立即隐藏,我希望它看起来像是用视图控制器推动。
  2. 当我尝试返回上一个视图控制器使用手势时,底栏会显示,即使我停止返回,也不会隐藏。
  3. 我知道一面旗帜hidesBottomBarWhenPushed。如果我使用该标志,每次需要推送视图控制器时,我都需要将其设置为YES。这有点复杂。

    有没有办法在自定义导航控制器中解决这些问题?

1 个答案:

答案 0 :(得分:0)

似乎解决了我的问题(代码不是很好,稍后会做一些更新):

- (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;
    }
}