将definePresentationContext与UIModalPresentationStyle.custom一起使用

sim*_*eon 10 uiviewcontroller ios uimodalpresentationstyle uipresentationcontroller

我使用视图控制器包含来管理一组子视图控制器,它们应该能够以自定义方式模态地呈现其他视图控制器.

我遇到了一个问题,当视图控制器使用时,不使用definesPresentationContext属性UIModalPresentationStyle.custom

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

ROOT
 |_ A
Run Code Online (Sandbox Code Playgroud)

A是的孩子ROOT.我想提出B的模态A,同时使用自定义UIPresentationController,UIViewControllerTransitioningDelegateUIViewControllerAnimatedTransitioning.

所以,我的里面的代码如下的控制器A(注控制器AdefinesPresentationContext设置true):

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

    let controllerToPresent = B()

    controllerToPresent.modalTransitionStyle = .custom
    controllerToPresent.transitioningDelegate = presentationController

    present(controllerToPresent, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

在这个函数中,我希望它是fromVC类型A,它实际上是ROOT.尽管事实上A规定definesPresentationContext.

所以我认为这是因为我正在使用UIModalPresentationStyle.custom.所以我把它改成了UIModalPresentationStyle.overCurrentContext

这会导致iOS从中正确读取definesPresentationContext属性A,animateTransition现在可以使用正确的视图控制器调用我的函数,但是:

因为我的模态表示风格不再存在.custom,所以我的转换委托中的以下方法不再被调用

func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
Run Code Online (Sandbox Code Playgroud)

所以我的演示控制器变得闲置.

我想要一种.custom尊重的模态过渡风格definesPresentationContext.这可能吗?我错过了什么吗?

基本上,我想在当前上下文中进行自定义模式演示.

cap*_*kaw 0

在您的UIPresentationController子类中,按如下方式重写shouldPresentInFullscreen

 override var shouldPresentInFullscreen: Bool {
     get {
         return false
     }
 }
Run Code Online (Sandbox Code Playgroud)

根据UIPresentationController标题:

// 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;
Run Code Online (Sandbox Code Playgroud)

definesPresentationContext应该可以解决问题。