iOS+TabBar的隐藏,hidesBottomBarWhenPushed的正确使用

一、前言

项目中在跳转子页面的时候隐藏tabbar是个很常见的需求,苹果也提供了方便的方法,即设置控制器的hidesBottomBarWhenPushed属性,但设置错误,就会出现莫名其妙的问题,曾经就掉入过坑中直到抓狂?

二、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.

???上面是苹果对属性hidesBottomBarWhenPushed的解释:???

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

三、例子:A ->B,A push 显示B,push时B底部的tabor隐藏

Demo.gif

1. 曾经错误的设置(以下的设置都是在A控制器中)

  1. 设置:在pushB之前,使 A.hidesBottomBarWhenPushed = Yes;

     // A push B
     - (IBAction)nextPage:(id)sender 
          // push之前设置A的hidesBottomBarWhenPushed属性
          BViewController *BVC = [[BViewController alloc] init];
          self.hidesBottomBarWhenPushed = YES;  //self为A控制器
          [self.navigationController pushViewController:BVC animated:YES];
     }    
    
  2. 结果:

    1. 在点击next的时候,确实成功跳转并隐藏了tabbar,满意?;

    2. 可返回的以后,tabbar隐藏了,这不是我想要的结果?;

    3. 那解决问题呗,在viewWillAppear的时候设置?

      // 解决返回时tabbar的隐藏问题
      - (void)viewWillAppear:(BOOL)animated{
            [super viewWillAppear:animated];
            self.tabBarController.tabBar.hidden = NO;
            self.hidesBottomBarWhenPushed = NO;
        }
      
    4. 设置后算是比较符合需求了,虽然效果怪怪的。本以为算是成功了,可再次push时又出问题了??????

2. 正确做法

??? 查查查资料。。。算是理解了,再看看苹果解释,有两个关键点
1.已经添加到导航控制器的子控制器;
2.最顶部的控制器
;

  1. 错误原因

    1. 系统在push B的时候,B控制器其实已经加入到导航控制器的子控制器中了
    2. 此时,B才是最顶部的控制器,B的hidesBottomBarWhenPushed属性才是正确控制tabbar隐藏的关键,而不是A的
  2. 既然找到原因,那就改:

    1. 清除先前的相关设置

       // 去除先前的设置,例如下面的
       A.hidesBottomBarWhenPushed = Yes;
       A.tabBarController.tabBar.hidden = NO;
       A.hidesBottomBarWhenPushed = NO;
      
    2. 正确设置:

       /// A push B
       - (IBAction)nextPage:(id)sender {
          // 设置B的hidesBottomBarWhenPushe
          BViewController *BVC = [[BViewController alloc] init];
          BVC.hidesBottomBarWhenPushed = YES;  // 设置B
          [self.navigationController pushViewController:BVC animated:YES];
       }
      
    3. 结果: 运行,多次测试,无误,放心了。?

    4. 上面的设置只需要在NavigationController的根控制器push到其他页面的时候设置一下就好,其他子页面都会隐藏tabbar,不用再在其他页面设置。

  3. 其实还可以整体设置下,重写UINavigationController中的push方法,免得每个tab下的控制器都设置。

    // 重写自定义的UINavigationController中的push方法
    // 处理tabbar的显示隐藏
    -(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
         if (self.childViewControllers.count==1) {
             viewController.hidesBottomBarWhenPushed = YES; //viewController是将要被push的控制器
         }
         [super pushViewController:viewController animated:animated];
    }

 


转载自:https://www.jianshu.com/p/4c94fc74f1e6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值