ビュー コントローラー コンテインメントを使用して、カスタムの方法で他のビュー コントローラーをモーダルに表示できる子ビュー コントローラーのセットを管理しています。
を使用してView Controllerから表示するときにdefinesPresentationContext
プロパティが使用されないという問題に遭遇しましたUIModalPresentationStyle.custom
ROOT
例として、 、A
、およびの3 つのビュー コントローラーがあります。B
ROOT
|_ A
A
の子ですROOT
。カスタム、、およびを使用してB
からモーダルに表示したいと思います。A
UIPresentationController
UIViewControllerTransitioningDelegate
UIViewControllerAnimatedTransitioning
したがって、コントローラーのコード内で次のことを行います(コントローラーが に設定されていることに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
definesPresentationContext
これにより、iOS は からプロパティを正しく読み取るようA
になり、animateTransition
ビュー コントローラーから正しい関数が呼び出されるようになりましたが、次のようになります。
モーダル プレゼンテーション スタイルが ではなくなったため.custom
、遷移中のデリゲートの次のメソッドが呼び出されなくなりました
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
したがって、私のプレゼンテーションコントローラーは使用されなくなります。
.custom
を尊重するモーダル遷移スタイルが必要ですdefinesPresentationContext
。これは可能ですか?何か不足していますか?
基本的に、現在のコンテキスト内でカスタム モーダル プレゼンテーションが必要です。