在我的代码中,我使用 present(:animated:completion:) 方法从 ViewController1 呈现 ViewController2,ViewController1 是嵌套在 RootViewController 中的容器 View 的 Root View Controller 。
我已将演示文稿样式设置为 .coverVertical,并且在其 viewDidLoad() 方法中将 ViewController1 的 definesPresentationContext 变量设置为 true。我还将 ViewController2 的演示文稿设置为 .overCurrentContext。这使得 ViewController2 的边界与 ViewController1 的边界相同,但无论出于何种原因,.coverVertical 动画从屏幕底部开始,而不是从 ViewController1 的框架底部开始。
但是,当我将容器 View 的 Root View Controller 设置为 UINavigationController 并在其中嵌套 ViewController1 时,这种情况就会消失。我假设这意味着我缺少一些第二个上下文变量来防止动画在其他 View 之上进行动画处理,但我似乎找不到除 definesPresentationContext 之外的任何其他变量。
最佳答案
所以你是说你的 View Controller 层次结构是
RootViewController
ViewController1
在这种情况下,在 ViewController1 中运行此代码:
let vc = // ViewController2 instance, obtained somehow
vc.modalTransitionStyle = .coverVertical
self.definesPresentationContext = true
vc.modalPresentationStyle = .currentContext
self.present(vc, animated: true)
您会看到只有 ViewController1 的 View 区域参与了转换。
请注意,容器 View 的clipsToBounds
必须设置为true
。如果没有容器 View ,请添加一个(以提供剪裁)——尽管根据您的描述,我相信有一个(即您在 Storyboard 中配置了它)。
关于swift - 容器 View 中的 definesPresentationContext = true 仍然允许在背景 View 上使用模态动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51601520/