新 iOS 13 模态呈现:呈现控制器不向下移动
New iOS 13 Modal Presentation: Presenting Controller Doesn't Move Down
在 iOS 13 中以模态方式呈现 UIViewController 时我有一个奇怪的行为。我在 iOS 13 中看到的新呈现样式如下所示:
呈现视图控制器出现在呈现视图控制器后面。它也向下移动以模仿 "stack"
同时,当通过我的应用程序呈现视图控制器时,我不断得到这种效果:
呈现新视图控制器时,呈现视图控制器根本没有移动
我使用这段代码来呈现这个视图控制器:
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
这是我的问题:
我想知道为什么会发生这种情况,以及是否有办法以正常的 iOS 13 样式呈现视图控制器(呈现视图控制器向后移动)。
提前致谢!
这应该是您唯一需要设置的 属性
presentedViewController.modalPresentationStyle = .automatic
如果没有 UINavigationController,我认为使用 vc.modalPresentationStyle = .fullScreen
可以解决问题,否则您可以使用以下代码:
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
present(vc, animated: true)
因为 iOS 13 这是一项新功能,Apple 已将视图控制器的默认呈现样式从 iOS 12[=12= 中的全屏更改为模态 sheet ]
原来问题出在我的视图控制器层次结构上。我能够通过使呈现视图控制器成为我的应用程序的根视图控制器来修复它。首先,我通过调用
将后台控制器设置为根视图控制器
window.rootViewController = self
然后使用我之前的代码
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
我介绍了视图控制器。感谢所有试图提供帮助的人!
我们可以在检查器工具栏中更改它。要实现它:转到 Inspector Tollbar 的第五部分,然后将 Presentation 字段更改为 Full Screen。
以编程方式:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
来自故事板:
就是这样。根本不需要玩根控制器或 window。
如需参考,请访问 this article。
iOS13:假设你有 3 个视图控制器:FirstVC、SecondVC、ThirdVC
FirstVC 向 SecondVC 提供以下代码:
let secondVC = SecondVC()
secondVC.modalPresentationStyle = .fullScreen
firstVC.present(secondVC, animated: true, completion: nil)
SecondVC 向 ThirdVC 提供以下代码:
let thirdVC = ThirdVC()
secondVC.present(thirdVC, animated: true, completion: nil)
将 secondVC 的 modalPresentationStyle 更改为 .currentContext
即可解决问题。
在 iOS 13 中以模态方式呈现 UIViewController 时我有一个奇怪的行为。我在 iOS 13 中看到的新呈现样式如下所示:
呈现视图控制器出现在呈现视图控制器后面。它也向下移动以模仿 "stack"
同时,当通过我的应用程序呈现视图控制器时,我不断得到这种效果:
呈现新视图控制器时,呈现视图控制器根本没有移动
我使用这段代码来呈现这个视图控制器:
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
这是我的问题: 我想知道为什么会发生这种情况,以及是否有办法以正常的 iOS 13 样式呈现视图控制器(呈现视图控制器向后移动)。
提前致谢!
这应该是您唯一需要设置的 属性
presentedViewController.modalPresentationStyle = .automatic
如果没有 UINavigationController,我认为使用 vc.modalPresentationStyle = .fullScreen
可以解决问题,否则您可以使用以下代码:
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = .fullScreen
present(vc, animated: true)
因为 iOS 13 这是一项新功能,Apple 已将视图控制器的默认呈现样式从 iOS 12[=12= 中的全屏更改为模态 sheet ]
原来问题出在我的视图控制器层次结构上。我能够通过使呈现视图控制器成为我的应用程序的根视图控制器来修复它。首先,我通过调用
将后台控制器设置为根视图控制器window.rootViewController = self
然后使用我之前的代码
let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)
我介绍了视图控制器。感谢所有试图提供帮助的人!
我们可以在检查器工具栏中更改它。要实现它:转到 Inspector Tollbar 的第五部分,然后将 Presentation 字段更改为 Full Screen。
以编程方式:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
来自故事板:
就是这样。根本不需要玩根控制器或 window。
如需参考,请访问 this article。
iOS13:假设你有 3 个视图控制器:FirstVC、SecondVC、ThirdVC
FirstVC 向 SecondVC 提供以下代码:
let secondVC = SecondVC()
secondVC.modalPresentationStyle = .fullScreen
firstVC.present(secondVC, animated: true, completion: nil)
SecondVC 向 ThirdVC 提供以下代码:
let thirdVC = ThirdVC()
secondVC.present(thirdVC, animated: true, completion: nil)
将 secondVC 的 modalPresentationStyle 更改为 .currentContext
即可解决问题。