我使用视图控制器遏制来管理一组子视图控制器,它应该能够以定制方式模态呈现其他视图控制器。与UIModalPresentationStyle.custom一起使用definesPresentationContext
我已经在使用UIModalPresentationStyle.custom
作为一个例子,从一个视图控制器呈现碰上其中definesPresentationContext
属性不使用一个问题,我有三个视图控制器:ROOT
,A
,和B
ROOT
|_ A
A
是ROOT
的子女。我想在使用自定义UIPresentationController
,UIViewControllerTransitioningDelegate
和UIViewControllerAnimatedTransitioning
时从A
模态地提供B
。
所以我做的里面的代码下面的控制器A
(注控制器A
有definesPresentationContext
设置为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
}
}
在此功能,在这里我希望fromVC
为A
型的,它实际上是ROOT
。尽管A
指定了definesPresentationContext
。
所以我想这是因为我使用UIModalPresentationStyle.custom
。所以我将其更改为UIModalPresentationStyle.overCurrentContext
这会导致iOS的从A
正确读取definesPresentationContext
财产,我animateTransition
功能现在被称为从视图控制器正确的,但:
因为我的模式呈现风格不再.custom
,在我的转变代表以下方法不再被调用
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
所以我的演讲控制器变为未使用。
我想要一个.custom
模式转换样式,其中涉及definesPresentationContext
。这可能吗?我错过了什么吗?
基本上,我想在当前上下文中进行自定义模态演示。
您是否尝试过在'A'中设置过渡委托? 在此行之前: 'present(controllerToPresent,animated:true,completion:nil)'。 试试这个: 'self.transitioningDelegate = presentationController' 我使用时,建议这样的:'UIModalPresentationStyle.overCurrentContext' – zero