结合使用definePresentationContext和UIModalPresentationStyle.custom [英] Using definesPresentationContext with UIModalPresentationStyle.custom

查看:49
本文介绍了结合使用definePresentationContext和UIModalPresentationStyle.custom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用视图控制器容器来管理一组子视图控制器,这些子视图控制器应该能够以 custom 方式模态呈现其他视图控制器.

我遇到了一个问题,当使用 UIModalPresentationStyle.custom 从视图控制器进行演示时, definesPresentationContext 属性未使用 >

作为一个例子,我有三个视图控制器: ROOT A B

  ROOT| _ A 

A ROOT 的子代.我想使用自定义 UIPresentationController UIViewControllerTransitioningDelegate UIViewControllerAnimatedTransitioning .

因此,我在控制器 A 的代码内执行以下操作(请注意,控制器 A definesPresentationContext 设置为 true ):

  func buttonPressed(_发件人:可以吗?){让presentationController = MyCustomPresentation()让controllerToPresent = B()controllerToPresent.modalTransitionStyle = .customcontrollerToPresent.transitioningDelegate = presentationController目前(controllerToPresent,动画:true,完成:nil)} 

但是,在演示控制器(也是我的 UIViewControllerAnimatedTransitioning )内部,我遇到以下问题:

  func animateTransition(使用transitionContext:UIViewControllerContextTransitioning){让fromVC = transitionContext.viewController(forKey:.from)让toVC = transitionContext.viewController(forKey:.to)如果让fromVC = fromVC as?一种,让toVC = toVC为?B {//从A到B进行演示}} 

在此函数中,我希望 fromVC 的类型为 A ,它实际上是 ROOT .尽管 A 指定了 definesPresentationContext .

所以我想这是因为我正在使用 UIModalPresentationStyle.custom .所以我将其更改为 UIModalPresentationStyle.overCurrentContext

这会导致iOS正确地从 A 读取 definesPresentationContext 属性,并且现在我的 animateTransition 函数已从视图控制器中使用正确的名称进行调用,但是:

因为我的模式表示样式不再是 .custom ,所以在我的过渡委托中的以下方法不再被调用

  funcpresentationController(forPresented提出:UIViewController,提出:UIViewController ?,来源:UIViewController)->UIPresentationController? 

所以我的演示控制器不再使用.

我想要一个尊重 definesPresentationContext .custom 模态转换样式.这可能吗?我想念什么吗?

基本上,我希望在当前上下文中有一个自定义的模态表示.

解决方案

在您的 UIPresentationController 子类中,按如下所示覆盖 shouldPresentInFullscreen :

 覆盖var shouldPresentInFullscreen:Bool {得到 {返回假}} 

按照 UIPresentationController 标头:

 //默认情况下,每个新演示文稿均为全屏显示.//可以使用以下方法重写此行为,以强制执行当前的上下文表示.//(预设值:YES)@property(非原子的,只读的)BOOL shouldPresentInFullscreen; 

这与 definesPresentationContext 一起可以解决问题.

I am using view controller containment to manage a set of child view controllers which should be able to modally present other view controllers in a custom manner.

I have run into an issue where the definesPresentationContext property is not used when a presenting from a view controller using UIModalPresentationStyle.custom

As an example, I have three view controllers: ROOT, A, and B

ROOT
 |_ A

A is the child of ROOT. I would like to present B modally from A while using custom UIPresentationController, UIViewControllerTransitioningDelegate, and UIViewControllerAnimatedTransitioning.

So I do the following inside the code for controller A (note controller A has definesPresentationContext set to true):

func buttonPressed(_ sender: Any?) {
    let presentationController = MyCustomPresentation()

    let controllerToPresent = B()

    controllerToPresent.modalTransitionStyle = .custom
    controllerToPresent.transitioningDelegate = presentationController

    present(controllerToPresent, animated: true, completion: nil)
}

However, inside my presentation controller (which is also my UIViewControllerAnimatedTransitioning) I encounter the following problem:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let fromVC = transitionContext.viewController(forKey: .from)
    let toVC = transitionContext.viewController(forKey: .to)

    if let fromVC = fromVC as? A,
        let toVC = toVC as? B {
        //Do the presentation from A to B
    }
}

In this function, where I expect fromVC to be of type A, it is actually ROOT. Despite the fact that A specifies definesPresentationContext.

So I figure this is because I'm using UIModalPresentationStyle.custom. So I change it to UIModalPresentationStyle.overCurrentContext

This causes iOS to correctly read the definesPresentationContext property from A, and my animateTransition function now gets called with the correct from view controller, but:

Because my modal presentation style is no longer .custom, the following method in my transitioning delegate no longer gets called

func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?

So my presentation controller becomes unused.

I want a .custom modal transition style which respects definesPresentationContext. Is this possible? Am I missing something?

Basically, I want a custom modal presentation within the current context.

解决方案

In your UIPresentationController subclass, override shouldPresentInFullscreen as follows:

 override var shouldPresentInFullscreen: Bool {
     get {
         return false
     }
 }

As per the UIPresentationController header:

// By default each new presentation is full screen.
// This behavior can be overriden with the following method to force a current context presentation.
// (Default: YES)
@property(nonatomic, readonly) BOOL shouldPresentInFullscreen;

This along with definesPresentationContext should do the trick.

这篇关于结合使用definePresentationContext和UIModalPresentationStyle.custom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆