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