How to animate transitions in a UITabBarController using more than 5 ViewControllers?

Viewed 162

I have a UITabBarController which has 9 view controllers and I am custom handling the navigation with a forward and back button. I have created a UIViewControllerAnimatedTransitioning object to handle the animation (simple left to right) and it gets returned in the delegate method:

tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

However, on and after selecting the fifth index, this function is no longer called (as it is now handled by the UIMoreNavigationController. Is there an animation delegate or some way I should be handling it for the UIMoreNavigationController instance?

1 Answers

You would need to specify the delegate of your moreNavigationController. So in your UITabBarController class you would need to:

        self.moreNavigationController.delegate = strongDelegate // where strongDelegate is a local instance of MyControllerDelegate as defined below

The delegate should then implement the navigationController function which will call the applicable UIViewControllerAnimatedTransitioning instance:

class MyControllerDelegate: NSObject, UINavigationControllerDelegate {

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    switch operation {
    case .push:
        return MyPresentationAnimationController(isPresenting: true)
    case .pop:
        return MyPresentationAnimationController(isPresenting: false)
    default:
        return nil
    }
}

}

Your animateTransition function contains the logic where the magic happens:

class MyPresentationAnimationController : NSObject, UIViewControllerAnimatedTransitioning {

private let isPresenting: Bool

init(isPresenting: Bool) {
    self.isPresenting = isPresenting
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return TimeInterval(UINavigationController.hideShowBarDuration)
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard
        let toView = transitionContext.view(forKey: .to),
        let fromView = transitionContext.view(forKey: .from)
        else {
            return
    }
    let containerView = transitionContext.containerView

    // TODO: Implement the logic with UIView.animateKeyframes here ...

}

}

Related