swift - 容器 View 中的 definesPresentationContext = true 仍然允许在背景 View 上使用模态动画

标签 swift uikit ios-animations

在我的代码中,我使用 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/

相关文章:

ios - UITextView 在行尾点击不应返回下一行中的单词

ios - UIButton 之间的滑动动画

ios - UITableView 阻尼动画和布局约束

swift - 了解闭包语法

objective-c - 在 Swift 协议(protocol)中使用 ObjC block

iphone - 如何将 UIScrollView 嵌套在 UIScrollView 中,以便用户可以滚动内部 UIScrollView?

iOS 在完成之前停止 animateWithDuration

ios - 从数组到字典分组

ios - UIButton 图像自动缩放

ios - 用换行符替换换行文本后获取 UILabel 的文本