如何在不使用 presentViewController 的情况下以模态方式呈现自定义视图?

How can I present a custom view modally without using presentViewController?

是否可以使用 addChildViewController 以模态方式呈现视图控制器?我想以模态方式呈现 UIviewcontroller's 视图,以便它与 window 层次结构下的任何视图重叠。我想在不使用 self.presentViewcontroller 的情况下执行此操作,因为我想构建 我自己的动画 而不是使用预设翻转 up/down/ 等。我尝试使用 self.addChildViewcontroller(modal)self.view.addSubView(modal.view) 但它进入我的 ChildViewController。当我使用 self.view.window?.addSubview 时,应用程序崩溃了。

是否可以使用 addChildController 模态呈现 UIViewcontroller?

我包含了我的示例代码。

https://github.com/cuongta/testcode

override func viewDidLoad() {
   super.viewDidLoad()

   var childVC = ChildViewController()
   var navVC = UINavigationController()
   navVC.viewControllers = [childVC]
   self.addChildViewController(navVC)
   self.view.addSubview(navVC.view)
}

你应该使用 UIViewControllerAnimatedTransitioningUIViewControllerTransitioningDelegate 来实现你想要的。

根据 Apple,我认为这是最好的方法。

Adopt the UIViewControllerAnimatedTransitioning protocol in objects that implement the animations for a custom view controller transition.

The methods in this protocol let you define an animator object, which creates the animations for transitioning a view controller on or off screen in a fixed amount of time. The animations you create using this protocol must not be interactive. To create interactive transitions, you must combine your animator object with another object that controls the timing of your animations.

您可以下载 Apple 示例代码 here