ios - 如何在不使用 PresentViewController 的情况下以模态方式呈现自定义 View ?

标签 ios swift uiviewcontroller

是否可以使用 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)
}

最佳答案

您应该使用 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

关于ios - 如何在不使用 PresentViewController 的情况下以模态方式呈现自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30320403/

相关文章:

ios - 使用 Swift 在 CoreLocation 中搜索区域的正确方法是什么?

iphone - 如何设计好看的 iPhone 应用程序

ios - 重新加载带有动画的表格 View 单元格(Swift)

ios - 当我在 Storyboard 模式下单击 Viewcontroller 时,布局会发生变化

ios - UIPageViewController 具有未知数量的 UIViewController 实例

ios - UISlider 跟踪标注未与拇指对齐

iphone - 存储查找日期选择(实际值或查找索引)的最佳方法是什么?

xcode - 获取解析日期,转换为字符串并在单元格详细信息中显示

Swift 协议(protocol)扩展 - 无法访问 Func

ios - View Controller 未加载 Storyboard View