0

I'm trying to make two different custom transition (one fade and one slide from top) within a navigation controller.
enter image description here

The transition works very well the first time but when pop the transitions acting crazy. I guess that navigationController.delegate is the key but I can't figure it out by myself. Any help will be greatly appreciated thanks a lot

******************************** IOS14 QUESTION UPDATE *****************************************

the solution of @Vlad for setting the delegate is working great thanks.

But recently in IOS14 a stack menu appearing when a long press gesture is detected on navigation back button (which allows user to navigate through the navigationcontroller's viewcontrollers stack).

And so the navigationcontroller delegate is setting to the wrong controller when popping two or more controllers.

I am once again asking for your support

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
guiaume
  • 67
  • 5
  • Can you prove more detail than just "acting crazy"? Which VC are you in when you pop; A, B, or C? Can you provide a code sample? – Vlad Oct 06 '18 at 13:07
  • Sure @Vlad, thank you for your answer. A is push to B with anim1, then B is push to C with anim2 everything works fine. On pop, C is pop to B with anim2 as expected, but B is pop to A with anim2 wich is wrong. And then if A is push to B again, it's push with the defaut animation (the slide in apple animation). I'm using simple "UIViewControllerAnimatedTransitioning", "navigationController?.delegate = self" is set in viewdidload, and " transitioningDelegate" is set in prepare for segue – guiaume Oct 06 '18 at 21:36
  • Thanks for that information. I've posted an answer based on the information you've provided. Let me know if you need more information to understand the problem. – Vlad Oct 07 '18 at 09:13

1 Answers1

1

Your starting state is in VC A. After viewDidLoad, the navigationController?.delegate is set to VC A which uses anim1. When you push to B, you're setting navigationController?.delegate to B, which uses anim2.

When you pop from C to B, anim2 is used as navigationController?.delegate is VC B. When you pop from B to A, anim2 is used because navigationController?.delegate is still VC B.

When you pop B, navigationController?.delegate is set to nil because the instance of VC B is destroyed. That is why when you try and push B again, the default animation is used.

Two important pieces of information is that:

  1. viewDidLoad is only called one time when the view has finished loading and not when it appears again after a pop.
  2. navigationController?.delegate can only point to one delegate.
Vlad
  • 932
  • 6
  • 18
  • Thank you for the explanation @Vlad. So do you think it's possible to make it work with NavigationController? Or do I have to make A to B with push and B to C with modall presentation to have two different animations? – guiaume Oct 07 '18 at 19:06
  • Yes, it's possible. You can set the delegate in viewDidAppear instead of viewDidLoad. That way the transition will be set up every time the VC appears. So if VC A appears, then anim1 would be used, and then when VC B appears anim2 would be used. Then when you pop back to VC A, the transition would be set to anim1 again. – Vlad Oct 09 '18 at 19:37