【问题标题】:How to use hidesBottomBarWhenPushed if i want to hide the tabbar for only one kind of controller?如果我只想为一种控制器隐藏标签栏,如何使用 hidesBottomBarWhenPushed?
【发布时间】:2020-01-22 13:37:59
【问题描述】:

我在使用 hidesBottomBarWhenPushed 时遇到了麻烦... 我将按顺序将三个控制器 A、B 和 C 推送到导航控制器中,并且我想在显示 B 时隐藏底部标签栏。(并且 A 是标签栏控制器之一)

有人有想法吗?

【问题讨论】:

    标签: ios uitabbar


    【解决方案1】:

    在视图控制器 A(位于 tabBar 上)中,当需要呈现 B(不需要 tabBar)时:

    self.hidesBottomBarWhenPushed = YES; // hide the tabBar when pushing B
    [self.navigationController pushViewController:viewController_B animated:YES];
    self.hidesBottomBarWhenPushed = NO; // for when coming Back to A
    

    在视图控制器 B 中,当需要呈现 C 时(再次需要 tabBar):

    self.hidesBottomBarWhenPushed = NO; // show the tabbar when pushing C
    [self.navigationController pushViewController:viewController_C animated:YES];
    self.hidesBottomBarWhenPushed = YES; // for when coming Back to B
    

    【讨论】:

    • 虽然这个答案完成了工作,但它滥用了 hidesBottomBarWhenPushed 属性。您应该在特定的 viewController 上设置所需的值。在这种情况下,B 应将其设置为 YES,A 和 C 应将其设置为 NO。并确保您在 init 方法中执行此操作。
    【解决方案2】:

    我没有在 viewDidLoad 中设置它,而是发现有时为时已晚。在 init 中设置它或覆盖 hidesBottomBarWhenPushed 以对没有底部工具栏的视图返回 YES。

    【讨论】:

      【解决方案3】:

      来自 hidesBottomBarWhenPushed 文档:

      如果是,底部栏保持隐藏,直到视图控制器 从堆栈中弹出。

      这意味着如果您不一定知道视图控制器将被推送的顺序,则需要堆栈中的所有视图控制器将其 hidesBottomBarWhenPushed 设置为 false,除了 topViewController。

      那我该怎么办

      1. 在推送新的视图控制器之前,我根据需要设置了它的 hidesBottomBarWhenPushed 属性
      2. 还在推送之前,我设置了 self.hidesBottomBarWhenPushed 以确保整个堆栈直到下一个堆栈将其属性设置为 false
      3. 在弹出之前,我会检查 tabBar 是否应该显示,并更新它的 hidesBottomBarWhenPushed

      这是 1 和 2 的一些代码)

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
          self.hidesBottomBarWhenPushed = false
      
          if (segue.identifier == "MyViewControllerWhoHidesTabBar") {
            let viewController: MyViewControllerWhoShowsTabBar = segue.destinationViewController as! MyViewControllerWhoShowsTabBar
            viewController.hidesBottomBarWhenPushed = true
          }
             // rest of implementation....
       }
      

      3) 我已将后退按钮操作覆盖为

        func backButtonClick(sender:UIButton!) {
          let viewControllers = self.navigationController!.viewControllers
          if let vc = viewControllers[viewControllers.count-2] as? MyViewController {
            if vc.isKindOfPageYouDontWannaShowTheTabBar() == true {
              vc.hidesBottomBarWhenPushed = true
            }
          }
          navigationController?.popViewControllerAnimated(true)
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-28
        • 2015-02-13
        • 2018-02-24
        • 1970-01-01
        • 2018-08-04
        相关资源
        最近更新 更多