首页 > 解决方案 > iOS 应用程序中使用自定义转换的 4 级 ViewController 导航

问题描述

我正在尝试构建一个具有 4 个不同级别的菜单,其中每个级别代表一个UIViewController. 我希望菜单及其每个级别都以模态方式呈现。目前,我通过让基本 ViewController (VC) 呈现另一个来做到这一点,而后者又呈现第三个呈现第四个(结构将是 ABCD,其中每个-代表UIViewController.present(_:animated:completion:)对相应级别的调用)。

然后我正在实现UIViewControllerTransitioningDelegate并为其提供自定义转换对象。这适用于前 3 个级别(即菜单按预期工作 ABC 级别),但是当我在第 4 级并试图将其关闭时(即从 ABCD 到 ABC ),B 级别是短暂的在播放解雇动画时显示而不是 C 级别。动画完成后,C级再次弹出B。

我怀疑问题在于以某种方式在层次结构中呈现超过 3 个 VC:s,但是当使用默认的解除动画而不是我正在使用的自定义动画时,不会发生这种情况。

也许我应该采用不同的导航方法,或者有什么方法可以规避这种行为?我确实需要菜单为“模态样式”,据我所知,这会阻止我使用 a UINavigationController(我不想要导航栏)。

高度赞赏帮助。

- - 编辑 - -

这是我正在尝试完成的“伪代码”示例,因为项目很大,我很难粘贴所有内容:

 class A: UIViewController: UIViewControllerTransitioningDelegate {
   func viewDidLoad(){...}
    .
    .
    .
   @objc func presentB(){
     self.present(B, animated: true, completion: nil)
   }

   func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomTransition(presenting: true)
   }
   func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomTransition(presenting: false)
   }

 }


class B: UIViewController: UIViewControllerTransitioningDelegate {
   func viewDidLoad(){...}
    .
    .
    .
   @objc func dismiss(){
     self.dismiss(animated: true, completion: nil)
   }

   @objc func presentC(){
     self.present(C, animated: true, completion: nil)
   }

   func animationController(forPresented:presenting:source:) -> UIViewControllerAnimatedTransitioning? { ... }

   func animationController(forDismissed:) -> UIViewControllerAnimatedTransitioning? {...}

}


class C: UIViewController: UIViewControllerTransitioningDelegate {
   func viewDidLoad(){...}
    .
    .
    .
   @objc func dismiss(){
     self.dismiss(animated: true, completion: nil)
   }

   @objc func presentD(){
     self.present(D, animated: true, completion: nil)
   }

   func animationController(forPresented:presenting:source:) -> UIViewControllerAnimatedTransitioning? { ... }

   func animationController(forDismissed:) -> UIViewControllerAnimatedTransitioning? {...}

}

class D: UIViewController {
   func viewDidLoad(){...}
    .
    .
    .
   @objc func dismiss(){
     self.dismiss(animated: true, completion: nil)
   }   
}

标签: iosswiftuiviewcontrolleruinavigationcontrolleruikit

解决方案


推荐阅读