4

I have created a custom transition animation for a modal view controller by implementing the methods in the UIViewControllerTransitioningDelegate protocol.

In iOS 8 and 9 the methods are called normally and the transition works. However, in iOS 7, the animationControllerForPresentedController:presentingController:sourceController: method never gets called. The animationControllerForDismissedController: method still gets called normally.

#import "MyModalTransitioningDelegate.h"
#import "MyModalFadeTransition.h"

@implementation MyModalTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source
{
    return [[MyModalFadeTransition alloc] init];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [[MyModalFadeTransition alloc] init];
}

@end

In the modal view controller (i.e. the "presented controller") I have the following in its -viewDidLoad method:

self.modalTransitionDelegate = [[OTModalTransitioningDelegate alloc] init]; // This is a custom strong private property due to `tranisitioningDelegate` being a weak property.
self.transitioningDelegate = self.modalTransitionDelegate;
self.modalPresentationStyle = UIModalPresentationCustom;

Setting the modalPresentationStyle doesn't seem to make any difference in any version of iOS. The method that isn't being called does say that it's available in iOS 7 so I'm not sure why it isn't being called.

The modal view controller is being presented with the following code in the presenting view controller:

[self presentViewController:self.modalViewController
                   animated:YES
                 completion:nil];
commscheck
  • 388
  • 5
  • 14

4 Answers4

55

If anyone comes across this in later years, and you're using Swift 3, make sure that your call isn't to "animationControllerForPresentedController".

As of Xcode 8.1, the compiler doesn't automatically recognize this problem and thus doesn't offer to convert the code to modern syntax.

The above code will compile but it won't be a protocol implementation. It will just be a custom, uncalled function. (Such are the hazards of optional protocol methods, and the myriad problems with Xcode's autocomplete.)

So, make sure you implement the protocol with Swift 3 syntax:

func animationController(forPresented presented: UIViewController,
        presenting: UIViewController,
        source: UIViewController)
        -> UIViewControllerAnimatedTransitioning?
{
    // ... return your cached controller object here.
}

And remember to set the presentation style on the View Controller to be presented:

self.modalPresentationStyle = .custom

And the delegate:

self.transitioningDelegate = self // or wherever
Womble
  • 4,607
  • 2
  • 31
  • 45
13

Another issue is that transitioningDelegate is a weak property. So you can assign to it, then have your transitioning delegate class be released before the transition has a chance to run. When the transition does run, the value of transitioningDelegate is nil, and your methods never get called.

To see this, do the following:

let myVC = UIViewController(nibName: nil, bundle: nil)
likesVC.transitioningDelegate = BackgroundFadesInPresentationDelegate(viewController: likesVC)
likesVC.modalPresentationStyle = .custom
present(likesVC, animated: true, completion: nil)

Then in your transition delegate class, add

deinit {
    print("deinit")
}

And see if that print statement is hit before the transition.

You will run into this problem if you use a freestanding class to implement the UIViewControllerTransitioningDelegate. This is why tutorials such as this one generally have you implement the transitioning delegate in the either in the view controller class itself or as an extension. Other things are keeping the view controller from getting released.

In general in Cocoa Touch anything named "...Delegate" will be a weak property, to help avoid retain cycles. You should make your own custom class delegate properties weak as well. There's a good section on this in Apple's Cocoa Core Competencies.

bcattle
  • 12,115
  • 6
  • 62
  • 82
  • In the second code block of my question you'll see I've covered this, I've got a custom strong property to hold a reference to my transition delegate. However this is something that could trip others up, so thanks for the contribution. :) – commscheck Apr 27 '17 at 02:49
  • I'm having the above problem (in iOS 11). My transitioningDelegate is definitely NOT being released, and my modalPresentationStyle IS set to .custom. Stranger still, the "animationController" functions in my delegate DO get called. It's just complete insanity... Any idea where else to look? – jbm Jun 17 '18 at 21:24
  • Also, logging from within animationController(forPresented...) indicates that the transitioningDelegate and modalTransitionStyle are correct (or at least, they're what I would expect—i.e., my delegate class and "4" or ".custom"). – jbm Jun 17 '18 at 22:06
4

Setting the transitioningDelegate in the presenting view controller's initialiser instead of the viewDidLoad method fixed the issue.

It appears in iOS 7 a view controller's transitioning logic is called before viewDidLoad, but is the other way around from iOS 8 onwards.

commscheck
  • 388
  • 5
  • 14
0

After digging, I found that my transitioningDelegate methods were not getting called because they were implemented in a protocol extension and the delegate methods must be @objc which is not supported in protocol extensions. My resolution was to move this into the concrete class and add the @objc tag

    @objc func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        ...


    }
    
    // interaction for dismissing from  view
    @objc func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
   ...   
   }

 
Logan Sease
  • 372
  • 3
  • 5