Container view disappearing on completeTransition: Container view disappearing on completeTransition: objective-c objective-c

Container view disappearing on completeTransition:


The container view disappearing on dismissal is correct behavior. Your mistake is adding the fromView to it.

You are incorrectly distinguishing whether this is presentation or dismissal and what you should do in each case. Simply use the two view controllers fromViewController and toViewController to tell them apart; on dismissal, the roles are reversed. On dismissal, do not add anything to the content view; the original presenter is still present and will be revealed by the removal of the container view.

So, on presentation, add only the toView to the container view. On dismissal, do not add anything to the container view. It's as simple as that.


My solution is setting modalPresentationStyle = .custom. It can be before or after the line transitioningDelegate = self.

TLDR:

This discussion regards to presenting transition only, because I didn't have much issue with dismissing transition.

My modal view is smaller than the screen and the presenting view is supposed to be shown in the background. But no, I got a totally black background instead of the presenting view as soon as transitionContext.completeTransition is called. I have a similar modal view with the presenting view in the background somewhere else in code, and it's totally working. After doing some comparison I eliminate all the other possibilities and narrow down to the difference of modalPresentationStyle = .custom, which has .fullScreen for the default value. I guess .fullScreen assumes the presented view takes full screen and decide to remove the presenter view (fromView) regardlessly.

One thing interesting with modalPresentationStyle = .custom: transitionContext.view(forKey: .from) returns nil while transitionContext.viewController(forKey: .from) still returns the presenting view controller.