我使用視圖控制器遏制來管理一組子視圖控制器,它應該能夠以定製方式模態呈現其他視圖控制器。與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