是否可以使用 addChildViewController
以模态方式呈现 View Controller ?我想以模态方式呈现 UIviewcontroller 的
View ,以便它与窗口层次结构下的任何 View 重叠。我想在不使用 self.presentViewcontroller
的情况下执行此操作,因为我想构建我自己的动画,而不是使用预设的向上/向下翻转/等。我尝试使用 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)
}
最佳答案
您应该使用 UIViewControllerAnimatedTransitioning
和 UIViewControllerTransitioningDelegate
来实现您想要的。
根据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
关于ios - 如何在不使用 PresentViewController 的情况下以模态方式呈现自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30320403/