8

我使用视图控制器遏制来管理一组子视图控制器,它应该能够以定制方式模态呈现其他视图控制器。与UIModalPresentationStyle.custom一起使用definesPresentationContext

我已经在使用UIModalPresentationStyle.custom

作为一个例子,从一个视图控制器呈现碰上其中definesPresentationContext属性不使用一个问题,我有三个视图控制器:ROOTA,和B

ROOT 
|_ A 

AROOT的子女。我想在使用自定义UIPresentationControllerUIViewControllerTransitioningDelegateUIViewControllerAnimatedTransitioning时从A模态地提供B

所以我做的里面的代码下面的控制器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) 
} 

然而,我的演讲控制器(这也是我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 
    } 
} 

在此功能,在这里我希望fromVCA型的,它实际上是ROOT。尽管A指定了definesPresentationContext

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

这会导致iOS的从A正确读取definesPresentationContext财产,我animateTransition功能现在被称为从视图控制器正确的,但:

因为我的模式呈现风格不再.custom,在我的转变代表以下方法不再被调用

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

所以我的演讲控制器变为未使用。

我想要一个.custom模式转换样式,其中涉及definesPresentationContext。这可能吗?我错过了什么吗?

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

+0

您是否尝试过在'A'中设置过渡委托? 在此行之前: 'present(controllerToPresent,animated:true,completion:nil)'。 试试这个: 'self.transitioningDelegate = presentationController' 我使用时,建议这样的:'UIModalPresentationStyle.overCurrentContext' – zero

回答

0

在你UIPresentationController子类,覆盖shouldPresentInFullscreen如下:

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

由于每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; 

这与definesPresentationContext沿着应该做的伎俩。

+1

谢谢您的回答。我检查了我的UIPresentationController子类,发现它已经为'shouldPresentInFullscreen'返回'false' - 但它仍然没有将视图控制器放在定义表示上下文的树的下方,而是取而代之。 – simeon