隐藏TabBar是个累人的活

Posted 小敏的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了隐藏TabBar是个累人的活相关的知识,希望对你有一定的参考价值。

最近进行跳转界面隐藏tabbar的时候遇到了一些坑,现在把它记录下来,如果有需要的朋友可以参考一下.

大家一般使用tabbar的时候,隐藏有两种方法.

一种是设置当前所处界面的隐藏属性

self.tabBarController.tabBar.hidden = YES;

这一种是在当前界面中设置tabbar的隐藏,一般用法是:

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

tabbar在界面需要显示的时候隐藏,需要消失的时候出现,这一种管理方法很实用,界面自己处理自己的tabbar,不会影响到其他的界面,但是当需要隐藏的界面特别多的时候会很麻烦.
还有一点需要注意的是当你使用这个方法设置了隐藏的时候,tabbar的位置会空出一块来

例如:
我是使用的xib,显示一个单独的界面,这是一个单独的tableView

技术分享
tableView

在我使用这种方法隐藏的时候,会出现下面的结果:

技术分享
显示出了空出的效果


界面下边空出了一块tabbar的高度,这一点是在使用的时候需要注意的,如果使用这种方式,需要在搭建界面的之前就设置好,避免回过头来设置导致一些坑.使用下面的方式设置不会出现这种问题.

第二种方式是设置在push的时候隐藏底部视图

viewController.hidesBottomBarWhenPushed = YES;

对于这一种方法,苹果官方文档的解释是:

A view controller added as a child of a navigation controller can display an optional toolbar at the bottom of the screen. The value of this property on the topmost view controller determines whether the toolbar is visible. If the value of this property is YES, the toolbar is hidden. If the value of this property is NO, the bar is visible.

对于这一段话理解是:
设置这一个值的ViewController,他和他push的往下每一层的viewControllertabbar都会被隐藏.

如果我们想要只在第一层显示tabbar,往下的每一层都不显示的话,只需要设置

NextViewController *avc = [[NextViewController alloc] init];
avc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:avc animated:YES];

即可实现.

如果说我需要在往下的第n层设置显示tabbar呢?
尝试发现,如果一个viewController上一层本身就没有显示tabbar,你不能用方法hidesBottomBarWhenPushed = NO;来让他再次显示出来

不过你可以使用

{
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

来显示tabbar,不过不可避免的,会出现上面说的那个坑.

还有一种比较极端的方法

NextViewController *avc = [[NextViewController alloc] init];
    avc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:avc animated:YES];
    avc.hidesBottomBarWhenPushed = NO;

意思就是只在这一个push方法中隐藏下个界面的tabbar,你可以在每一个界面跳转的时候使用肉夹馍方式来控制自己想要的效果,不过这样就太麻烦了,失去了我们本来追求简便的意义,用来满足特定需求还不错.

所以只需要知道这个方法的原理,使用起来可以很灵活.也省去很多力气.



 

以上是关于隐藏TabBar是个累人的活的主要内容,如果未能解决你的问题,请参考以下文章

前端Vue项目:旅游App-city:隐藏TabBar的2种方法

使用tabBar跳转页面并隐藏tabBar

隐藏 TabBar 后无法触摸 TabBar 后面的区域

iOS 隐藏Tabbar

IOS 隐藏tabBar

iOS隐藏tabBar的方法