OC版本的:
//1.设置self.tabBarController.tabBar.hidden=YES;
self.tabBarController.tabBar.hidden=YES;
//2.如果在push跳转时需要隐藏tabBar,设置self.hidesBottomBarWhenPushed=YES;
self.hidesBottomBarWhenPushed=YES;
NextViewController *next=[[NextViewController alloc]init];
[self.navigationController pushViewController:next animated:YES];
self.hidesBottomBarWhenPushed=NO;
//注意:在push后设置self.hidesBottomBarWhenPushed=NO;
其实在OC中直接对next.hidesBottomBarWhenPushed=YES就可以在跳转界面的时候隐藏tabbar
而不需要设置self.hidesBottomBarWhenPushed=NO;
Swift版本的:
let testVC = UIViewController() self.hidesBottomBarWhenPushed = true self.navigationController?.pushViewController(testVC,animated: true) self.hidesBottomBarWhenPushed = false如果是push到下个界面就没有问题,如果从第二个界面push到第三个界面,再返回第二个界面,tabbar就会显示,这样还是达不到我们想要的结果
下面是我自己对hidesBottomBarWhenPushed 属性设置的一个总结,应该每一次push,都去执行对hidesBottomBarWhenPushed属性进行设置,如
果要push的界面多了,那不是做很多繁琐的工作,所以自定义了UINavigationController,对hidesBottomBarWhenPushed属性进行统一设置
import UIKit class BaseNavigationController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //每一次push都会执行这个方法,push之前设置viewController的hidesBottomBarWhenPushed override func pushViewController(_ viewController: UIViewController,animated: Bool) { viewController.hidesBottomBarWhenPushed = true super.pushViewController(viewController,animated: true) viewController.hidesBottomBarWhenPushed = false } override func popViewController(animated: Bool) -> UIViewController? { //以下函数是对返回上一级界面之前的设置操作 //每一次对viewController进行push的时候,会把viewController放入一个栈中 //每一次对viewController进行pop的时候,会把viewController从栈中移除 if self.childViewControllers.count == 2 { //如果viewController栈中存在的ViewController的个数为两个,再返回上一级界面就是根界面了 //那么要对tabbar进行显示 let controller:UIViewController = self.childViewControllers[0] controller.hidesBottomBarWhenPushed = false } else { //如果viewController栈中存在的ViewController的个数超过两个,对要返回到的上一级的界面设置hidesBottomBarWhenPushed = true //把tabbar进行隐藏 let count = self.childViewControllers.count-2 let controller = self.childViewControllers[count] controller.hidesBottomBarWhenPushed = true } return super.popViewController(animated: true) } }原文链接:https://www.f2er.com/swift/321531.html