您的位置:首页 > 移动开发 > IOS开发

iOS hidesBottomBarWhenPushed的正确用法

2017-07-19 12:18 429 查看
项目中在跳转子页面的时候隐藏tabbar是个很常见的需求,苹果也提供了方便的方法,即设置控制器的hidesBottomBarWhenPushed属性,但设置错误,就会出现莫名其妙的问题,曾经就掉入过坑中。

为了了解这个属性,首先看一下苹果文档里是怎么介绍它的。

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.

大意是:已经添加到导航控制器的子控制器可选择性的展示屏幕底部的toolbar。 最顶部的子控制器的属性值(hidesBottomBarWhenPushed)决定toolbar是否可见,如果属性值为YES,toolbar隐藏,为NO,则可见。

但是这里却分了几种push的方式,首先是使用Storyboard的:

第一种:xib加载或者Storyboard用identifier获取Controller

UIViewController *v2 = [self.storyboard instantiateViewControllerWithIdentifier:@"v2"];
v2.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:v2 animated:YES];

这种方式没什么好说的,基本上不会出问题。

第二种:拉线,也就是Storyboard用performSegue

self.hidesBottomBarWhenPushed = YES;
[self performSegueWithIdentifier:@"tov2" sender:nil];
self.hidesBottomBarWhenPushed = NO;

经测试证明,此种方式只会对后面的一级生效,继续往后Push还会出现TabBar,要继续往后push也隐藏Tabbar还得使用第三种方法,也建议如此!

第三种:拉线,在prepareForSegue函数里

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}

更方便的做法是:如果你在用 Storyboard,可以在 ViewController 的设置面板中把 Hide Bottom Bar on Push 属性勾选上,效果和上文代码一样。

如果你是用代码撸的界面,那么你只需要在NavigationController的rootViewController上,在push第一级页面的时候加上hidesBottomBarWhenPushed即可,之后的第二级第三级以及更多的push就算不加也会自动隐藏的。

MessageCenterTableViewController * cc = [[MessageCenterTableViewController alloc] init];
cc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:cc animated:YES];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息