peco peco - 4 months ago 15
iOS Question

Trigger viewWillAppear when using a transitioning delegate

I want to present a view controller modally using custom transitions:

Attempt 1

presentedViewController.modalPresentationStyle = .Custom
presentedViewController.transitioningDelegate = someTransitioningDelegate


This does the job well, but I want
viewWillAppear
to be called on the a view controller behind
presentedViewController
when I call
presentedViewController.dismissViewControllerAnimated(true)
. So I did this:

Attempt 2

presentedViewController.modalPresentationStyle = .FullScreen
presentedViewController.transitioningDelegate = someTransitioningDelegate


or

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


Now
viewWillAppear
gets called on the view controller behind
presentedViewController
when dismissing
presentedViewController
, and the transition occurs as expected when presenting the view controller.

However, when dismissing the view controller, the background during the transition is black; seems that
.FullScreen
causes the view controllers behind
presentedViewController
to be removed from the view hierarchy - which is good because presumably that's what triggers the
viewWillAppear
call.

Attempt 3

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


Interestingly enough, if
.CoverVertical
is specified, but the
transitioningDelegate
is not, then the background during the transition actually is the view controller located behind
presentedViewController
, but
viewWillAppear
gets called on the view controller behind
presentedViewController
anyway
. The only thing this is missing is the 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


Answer

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.

Comments