我想在将新视图控制器推入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];
问题:
我知道一面旗帜hidesBottomBarWhenPushed
。如果我使用该标志,每次需要推送视图控制器时,我都需要将其设置为YES
。这有点复杂。
有没有办法在自定义导航控制器中解决这些问题?
答案 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;
}
}