简体   繁体   English

新 iOS 13 模态演示:演示 Controller 不向下移动

[英]New iOS 13 Modal Presentation: Presenting Controller Doesn't Move Down

I have a weird behavior when presenting UIViewControllers modally in iOS 13. The new presentation style that I've seen all across iOS 13 looks like this:在 iOS 13 中以模态方式呈现 UIViewControllers 时,我有一个奇怪的行为。我在 iOS 13 中看到的新呈现风格如下所示:

The presenting view controller appears behind the presented view controller.呈现视图 controller 出现在呈现视图 controller 后面。 It is also shifted down to mimic a "stack"它也被向下移动以模仿“堆栈”

呈现视图控制器出现在呈现视图控制器的后面。它也被向下移动以模仿“堆栈”

Meanwhile, when presenting view controllers through my app, I keep getting this effect:同时,当通过我的应用程序呈现视图控制器时,我不断得到这种效果:

The presenting view controller doesn't move at all when presenting a new view controller呈现新视图 controller 时,呈现视图 controller 根本不动

呈现新视图控制器时,呈现视图控制器根本不移动

I use this code to present this view controller:我使用此代码来呈现此视图 controller:

let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)

Here is my question: I'm wondering why this is happening and if there is a way to present view controllers in the normal iOS 13 style (with the presenting view controller moving back).这是我的问题:我想知道为什么会发生这种情况以及是否有办法以正常的 iOS 13 样式呈现视图控制器(呈现视图 controller 向后移动)。

Thanks in advance!提前致谢!

Turns out the problem was my view controller hierarchy.原来问题是我的观点 controller 层次结构。 I was able to fix it by making the presenting view controller the root view controller of my app.我可以通过将呈现视图 controller 作为我的应用程序的根视图 controller 来修复它。 First I set the background controller as the root view controller by calling首先,我通过调用将背景 controller 设置为根视图 controller

window.rootViewController = self

and then using my previous code然后使用我以前的代码

let controller = storyboard?.instantiateViewController(withIdentifier: "tutorial") as! TutorialController
controller.modalPresentationStyle = .pageSheet
controller.modalTransitionStyle = .coverVertical
present(controller, animated: true, completion: nil)

I presented the view controller.我展示了 controller 视图。 Thanks to everyone who tried to help!感谢所有试图提供帮助的人!

I think the issue can be resolved by using vc.modalPresentationStyle =.fullScreen if there is not UINavigationController, otherwise you can use these codes as follows:如果没有 UINavigationController,我认为可以使用vc.modalPresentationStyle =.fullScreen解决问题,否则可以使用以下代码:

let navigationController = UINavigationController(rootViewController: vc) 
navigationController.modalPresentationStyle = .fullScreen 
present(vc, animated: true)

because With iOS 13 this is a new feature that Apple has changed the default presentation style of View Controllers to a modal sheet from fullscreen in iOS 12因为对于 iOS 13,这是一项新功能,Apple 已将视图控制器的默认呈现样式从 iOS 12 中的全屏更改为模式表

We can change it in the Inspector Tool Bar.我们可以在 Inspector Tool Bar 中更改它。 To Achieve it: head over to the fifth section of Inspector Tollbar then change the Presentation field to Full Screen.要实现它:转到 Inspector Tollbar 的第五部分,然后将 Presentation 字段更改为 Full Screen。

Programmatically:以编程方式:

let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)

From storyboard:从 storyboard:

在此处输入图像描述

That's it.而已。 No need to play with root controller or window at all.根本不需要玩根 controller 或 window。

For reference, visit this article .如需参考,请访问本文

iOS13: Let's say you have 3 view controllers: FirstVC, SecondVC, ThirdVC iOS13:假设你有 3 个视图控制器:FirstVC、SecondVC、ThirdVC

FirstVC presents SecondVC with the below code: FirstVC 向 SecondVC 提供以下代码:

let secondVC = SecondVC()
secondVC.modalPresentationStyle = .fullScreen
firstVC.present(secondVC, animated: true, completion: nil)

SecondVC presents ThirdVC with the below code: SecondVC 为 ThirdVC 提供以下代码:

let thirdVC = ThirdVC()
secondVC.present(thirdVC, animated: true, completion: nil)

Changing secondVC's modalPresentationStyle to .currentContext solves the problem.将 secondVC 的 modalPresentationStyle 更改为.currentContext解决问题。

This should be the only property you need to set这应该是您需要设置的唯一属性

presentedViewController.modalPresentationStyle = .automatic

Detailed in https://developer.apple.com/videos/play/wwdc2019/224/详细见https://developer.apple.com/videos/play/wwdc2019/224/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 controller 在 iOS 中呈现模态时 UINavigationBar 高度 13 - UINavigationBar height when controller presenting modal in iOS 13 在 iOS 13 全屏中呈现模态 - Presenting modal in iOS 13 fullscreen iOS Swift-在呈现新的View Controller时呈现View Controller向下滑动 - iOS Swift - Presenting View Controller sliding down while presenting new View Controller iOS 13 UIViewController 模态表示阴影 - iOS 13 UIViewController modal presentation shadow iOS 13模态演示的控制高度 - Control height of iOS 13 modal presentation UILabel不会通过模式表示视图控制器进行更新 - UILabel doesn't update through modal presentation view controller 在iOS 7中,使用UIModalPresentationCurrentContext进行动画模式演示不会设置动画 - In iOS 7, using UIModalPresentationCurrentContext for an animated modal presentation doesn't animate NSInternalInconsistencyExceptionException:指定的模式表示样式没有相应的表示控制器 - NSInternalInconsistencyException: The specified modal presentation style doesn't have a corresponding presentation controller iOS呈现基于模式和导航控制器的视图 - iOS presenting modal and navigation controller based views 在iOS中呈现模态视图控制器不起作用 - Presenting Modal View Controller in iOS not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM