【问题标题】:Interactive transition: navigation bar appearance issue交互过渡:导航栏外观问题
【发布时间】:2017-12-07 03:16:06
【问题描述】:

假设,我有 3 个视图控制器:ABC,都嵌入到导航控制器中。 AB 有导航栏,C 没有。

我在BC 之间有一个自定义的交互过渡。由于我需要我的导航栏在C 上消失,所以我实现了UINavigationControllerDelegate 的这个功能:

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
    if viewController is C {
        navigationController.setNavigationBarHidden(true, animated: animated)
    }
    else {
        navigationController.setNavigationBarHidden(false, animated: animated)
    }
}

在一个常见的场景中,一切都很完美,当我只进行推-弹出转换时。

但是当我通过在UIPercentDrivenInteractiveTransition 上调用cancel()取消转换B->C 时,导航栏不会显示在B 上。在这里我必须打电话给setNavigationBarHidden(false, ...),但我还没有找到正确的地方来做这件事。

如果我在BviewWillAppear 中调用它,导航栏会出现,但看起来很奇怪——它包含C 如果有导航栏就会有的元素。如果我弹回A,它会闪烁一会儿,显示预期的内容,但在转换后A 导航栏立即被B 导航栏取代!

因此,似乎导航栏堆栈在B->C 转换取消后以某种方式损坏,它似乎相对于视图控制器进行了这样的移动:

                      has
-----------------------------------------------
|    ViewController    |   Navigation bar of  |
-----------------------------------------------
|           A          |           B          |
-----------------------------------------------
|           B          |           C          |
-----------------------------------------------

那么,我的问题是在这种情况下调用navigationController.setNavigationBarHidden(false, animated: true) 的正确位置是什么?

【问题讨论】:

    标签: ios iphone swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    好吧,我已经设法找到了一个丑陋的黑客来自己修复它。也许这个世界上有人会觉得它有帮助。

    • 在我的自定义 UIPercentDrivenInteractiveTransition 中,我像这样覆盖 cancel 函数:

      class CustomTransitionManager: UIPercentDrivenInteractiveTransition {
      
          /// Indicates transition direction. Must be set before each transition.
          var forward: Bool = true
      
          /// Current navigation controller used for transition. Must be set before transition starts
          var nc: UINavigationController?
      
          /**
           * Hack #1 with UINavigationController here
           */
          override func cancel() {
              super.cancel()
      
              if forward {
                  self.nc?.setNavigationBarHidden(false, animated: false)
              }
              self.nc?.setNavigationBarHidden(forward, animated: false)
          }
      }
      
    • 在每个视图控制器(A、B、C)中,我做了以下 hack:

      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
      
          // Hide and immediately show navigation bar: this will restore it's correct state
          self.navigationController?.setNavigationBarHidden(true, animated: false)
          self.navigationController?.setNavigationBarHidden(false, animated: true)
      }
      

    最好的解决方案可能是C 的模态全屏演示,但在我的情况下,我正在处理一个已经损坏导航层次结构的项目,我没有时间正确修复它。基本上,这就是我遇到这个问题的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多