【问题标题】:Hide TabBar when pushed into the navigation stack and bring it back when popped out of the navigation stack推入导航堆栈时隐藏 TabBar 并在弹出导航堆栈时将其返回
【发布时间】:2011-06-26 07:48:30
【问题描述】:

我正在尝试执行以下操作。

我有一个标签栏控制器,里面有 2 个标签。这两个选项卡都是导航控制器,每个选项卡上都有一个表格视图。

现在,当我在第一个选项卡中选择表格的一个单元格时,我正在推动另一个选项卡栏控制器,所以我想隐藏父选项卡控制器的选项卡栏,并且当我单击导航栏上的后退按钮时我想再次看到父标签栏,就像我在父标签栏视图中一样。

我试过 hidesbottombarwhenpushed,它隐藏了父标签栏控制器标签栏,但是当我点击返回时,它并没有返回。

【问题讨论】:

    标签: ios objective-c swift uitabbarcontroller uitabbar


    【解决方案1】:

    好的,所以我终于得到了答案,这就是我应该做的。

    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:aViewController animated:YES];
    self.hidesBottomBarWhenPushed=NO;
    

    所以基本上 hidesBottomBarWhenPushed = YES,然后推送你的视图控制器,然后 hidesBottomBarWhenPushed = NO;这就像一个魅力。

    感谢eddy 和他的帖子here

    【讨论】:

    • 它有效,但是当我点击导航栏后退按钮时。它再次出现。我希望它只出现在 firstview 上。
    【解决方案2】:

    接受的答案对我来说有问题。

    我的应用有一个深度为三个 UIViewController 的导航。

    • FirsViewController 显示的是 UITabBar。 (正确)
    • FirsViewController 推送 SecondViewController,SecondViewController 不显示 UITabBar。 (正确)
    • SecondViewController 推送ThirdViewController,ThirdViewController 显示UITabBar。 (不正确)
    • ThirdViewController 弹出到 SecondViewController,SecondViewController 显示的是 UITabBar。 (不正确)
    • SecondViewController 弹出到 FirstViewController,FirstViewController 显示为 UITabBar。 (正确)

    我的解决方案是设置 UINavigationControllerDelegate 的委托

    迅速:

    self.navigationController?.delegate = self
    

    目标-c:

    self.navigationController.delegate = self;
    

    然后实现下面的委托方法

    斯威夫特:

    fun navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    
        if fromVC.isKindOfClass(FirstViewController) && toVC.isKindOfClass(SecondViewController) {
    
            self.hidesBottomBarWhenPushed = true;
    
        }
        else if fromVC.isKindOfClass(SecondViewController) && toVC.isKindOfClass(FirstViewController) {
    
            self.hidesBottomBarWhenPushed = false;
    
        }
    
        return nil
    
    }
    

    目标-c:

    -(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                     animationControllerForOperation:(UINavigationControllerOperation)operation
                                                  fromViewController:(UIViewController*)fromVC
                                                    toViewController:(UIViewController*)toVC
    {
    
        if ([fromVC isKindOfClass:[FirstViewController class]] && [fromVC isKindOfClass:[SecondViewController class]]) {
    
            self.hidesBottomBarWhenPushed = true;
    
        }
        else if ([fromVC isKindOfClass:[SecondViewController class]] && [fromVC isKindOfClass:[FirstViewController class]]) {
    
            self.hidesBottomBarWhenPushed = false;
    
        }
    
        return nil;
    
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      正如 Apple 文档所述,您不能将 UITabBarController 实例推送到 NavigationController 上。这有一个很好的理由:如果您在标签栏中选择了另一个项目,您如何从推送的标签栏控制器返回?

      简单的答案是:不要那样做,它会让您的用户感到困惑。您可以尝试将第一个视图控制器交换为另一个可能是选项卡栏控制器的视图控制器,但不要使用推送范例:使用显式按钮代替将您的第一个选项卡栏控制器交换为第二个,最好使用视觉过渡。

      您可以查看UIView 类的setAnimationTransition:forView:cache: 文档,了解如何将标签栏控制器换成另一个:

      1. 开始一个动画块。
      2. 在容器视图上设置过渡。
      3. 从容器视图中移除子视图。
      4. 将新的子视图添加到容器视图中。
      5. 提交动画块。

      在这种情况下,容器视图将是应用程序的窗口。

      【讨论】:

      • 当我将一个 UITabbarController 推到导航控制器上时,假设 UITabBarController 有三个选项卡,所以我假设第一个选项卡将有一个返回按钮以返回父视图。如果用户在任何其他选项卡上,我知道他无法返回父视图,我想我可以接受。所以我想知道我该怎么做
      【解决方案4】:

      您也可以在选择 tabBar 控制器时使用属性检查器将其隐藏

      【讨论】:

        【解决方案5】:

        在将要推送的控制器中设置hidesBottomBarWhenPushed = true

        用于隐藏所有放入prepare for segue的控制器

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            segue.destination.hidesBottomBarWhenPushed = true
        }
        

        【讨论】:

          【解决方案6】:

          在您的 FirstViewController 中使用

          self.hidesBottomBarWhenPushed = true
          

          在您的 SecondViewController 中使用

          override func willMoveToParentViewController(parent: UIViewController?) {
            if parent == nil {
              var viewControllers = self.navigationController!.viewControllers
              if ((viewControllers[viewControllers.count - 2]).isKindOfClass(FirstViewController.self)) {
                (viewControllers[viewControllers.count - 2] as! FirstViewController).hidesBottomBarWhenPushed = false
              }
            }
          }
          

          【讨论】:

            猜你喜欢
            • 2018-04-12
            • 2017-11-25
            • 1970-01-01
            • 2021-11-25
            • 2021-03-26
            • 1970-01-01
            • 1970-01-01
            • 2013-02-19
            • 2019-03-08
            相关资源
            最近更新 更多