[转]iOS中hidesBottomBarWhenPushed的正确用法(tabbar嵌套nav)

参考:http://www.isaced.com/post-223.html

主要是在TabBar嵌套Nav时,进行Push的时候隐藏TabBar的问题。

说说hidesBottomBarWhenPushed,从这个属性名也能知道它的意思了,官方的解释是这样:

If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.

最外面是一个TabBarController,套了两个NavgationController,当其中一个VC push下去的时候,一般情况是这样:

1466135232-7460-99124a25c2b03f86b970fed52485
当隐藏Tabbar的时候再push,效果是这样:

1466135238-2595-ad08df96c52d42476df884ee7219
 

为了解决这个问题。代码非常简单,一句或者两句话即可,这里得分几种Push的情况。

Case1:xib加载或者Storyboard用identifier获取Controller

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

Case2:拉线,也就是Storyboard用performSegue

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

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

Case3:拉线,在prepareForSegue函数里

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *