Using definePresentationContext with UIModalPresentationStyle.custom

I use the view controller shell to manage a set of child view controllers, which should be able to present other view controllers in a different way.

I had a problem when the definesPresentationContext property definesPresentationContext not used when the presentation from the view controller using UIModalPresentationStyle.custom

As an example, I have three view controllers: ROOT , A and B

 ROOT |_ A 

A is a child of ROOT . I would like to introduce B modally from A using custom UIPresentationController , UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning .

So, I am doing the following inside the code for controller A (note A controller has definesPresentationContext set to true ):

 func buttonPressed(_ sender: Any?) { let presentationController = MyCustomPresentation() let controllerToPresent = B() controllerToPresent.modalTransitionStyle = .custom controllerToPresent.transitioningDelegate = presentationController present(controllerToPresent, animated: true, completion: nil) } 

However, inside my view controller (which is also my UIViewControllerAnimatedTransitioning ), I encounter the following problem:

 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 } } 

In this function, where I expect fromVC to be of type A , it is actually ROOT . Even though A points to definesPresentationContext .

So, I believe that this is because I use UIModalPresentationStyle.custom . So I change it to UIModalPresentationStyle.overCurrentContext

This leads to the fact that iOS correctly reads the definesPresentationContext property from A , and now the animateTransition function animateTransition called with the correct one from the presentation controller, but:

Since my modal presentation style is no longer .custom , the next method in my delegate sharing is no longer called

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

So my presentation controller is not used.

I need a .custom modal transition .custom that takes into account definesPresentationContext . Is it possible? Am I missing something?

Basically, I want to create a custom modal presentation in the current context.

+8
ios uiviewcontroller uipresentationcontroller
source share
1 answer

In your subclass of UIPresentationController override shouldPresentInFullscreen as follows:

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

According to the header of the 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; 

This along with definesPresentationContext should do the trick.

0
source share

All Articles