Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger viewWillAppear when using a transitioning delegate

I want to present a view controller:

  • Modally
  • Using custom transitions
  • At the same time, I want to make sure that when it is dismissed, the view controller behind it is aware of being pushed to the foreground.

My basic idea is to try some different configurations and see which one will lead to viewWillAppear being called on the view controller behind.

Attempt 1

presentedViewController.modalPresentationStyle = .Custom
presentedViewController.transitioningDelegate = someTransitioningDelegate

The results of this approach:

  • The custom transition works perfectly well
  • viewWillAppear does not get called on the view controller behind presentedViewController when I call presentedViewController.dismissViewControllerAnimated(true)

I do want viewWillAppear to be called on the view controller below the one being dismissed, so I did this:

Attempt 2

presentedViewController.modalPresentationStyle = .FullScreen
presentedViewController.transitioningDelegate = someTransitioningDelegate

or

presentedViewController.modalPresentationStyle = .FullScreen
presentedViewController.modalTransitionStyle = .CoverVertical
presentedViewController.transitioningDelegate = someTransitioningDelegate

The results of this approach:

  • viewWillAppear gets called on the view controller behind presentedViewController when dismissing presentedViewController
  • The transition occurs as expected when presenting the view controller.
  • When dismissing the view controller, the background during the transition is black, which is undesirable.

Seems that .FullScreen causes the view controllers behind presentedViewController to be removed from the display hierarchy - which is good because presumably that's what triggers the viewWillAppear call.

Attempt 3

presentedViewController.modalPresentationStyle = .FullScreen
presentedViewController.modalTransitionStyle = .CoverVertical

The results of this are:

  • viewWillAppear gets called on the view controller behind presentedViewController.
  • The background during the transition is the view controller located behind presentedViewController, which is desired.
  • No custom transition.

The project I'm working on is structured in a way that makes it difficult to use delegation (which seems to be the suggested answer here). Making use of NSNotificationCenter is another alternative which lets me call the code that is supposed to be called by viewWillAppear, but from attempt 3, I'm hoping there is a more elegant approach to achieve all these:

  1. Trigger viewWillAppear
  2. Use a custom transition
  3. See the view controller being presented in the background during the transition animation
like image 479
peco Avatar asked Feb 11 '26 13:02

peco


1 Answers

Seems Apple considers it foul play to invoke viewWillAppear etc., but it's okay to invoke beginAppearanceTransition and endAppearanceTransition, which in turn will invoke viewWillAppear. I'm going with this.

like image 126
peco Avatar answered Feb 14 '26 02:02

peco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!