Achieving a smooth custom transition between view controllers swift

800 Views Asked by At

enter image description herecan anyone help me fix this annoying jump/glitch bug when using a custom transition on view controller to view controller. I can't use push because I want to display the top navigation bar but hide the bottom tab bar so I have had to use a custom transition. I want to acheive a smooth transition like when going from view controller to view controller on a navigation controller. I have attached my code.

 func presentDetail(_ viewControllerToPresent: UIViewController) {
        let transition = CATransition()
        transition.duration = 0.25
        transition.type = CATransitionType.push
        transition.subtype = CATransitionSubtype.fromRight
        self.view.window!.layer.add(transition, forKey: kCATransition)
        present(viewControllerToPresent, animated: false)
    }
2

There are 2 best solutions below

0
Shawn Frank On

This is not so different from the comments given, however just some explanations that were not easy to add in comments.

I don't think what you see is a glitch, but a fade that is automatically applied with CATransition - you can see it slowed down.

Custom Push Transition UIViewController

You could go the CABasicAnimation or customizing UIViewControllerTransitions CATransition swift iOS but these will be a lot more work

I am not sure why hidesBottomBarWhenPushed does not work for you.

I have the following view hierarchy:

UITabBarController
   - UINavivationController (Tab 1)
     - UIViewController
   - UIViewController2 (Tab 2)

I now do this:

let dtvc = DetailTransitionVC()
dtvc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(dtvc, animated: true)

And this seems to give me what you want:

UINavigationController push without tab bar remove iOS Swift

0
faraaz On

I was just experiencing this behavior as well. The culprit lied in how I was transitioning. I was pushing a VC using this function because I also wanted to support fade transitions. Previously, I was using .push as the AnimationType to apply the transition. This was causing that weird effect you see in the GIF. Instead now, I made animation an optional which only will applyTransition if it is not undefined. Otherwise, it will use the default built in transition which is smooth.

open func push(vc: UIViewController, animation: AnimationType? = nil) {
    guard let viewController = self.viewController else { return }
    
    if let animation = animation {
        applyTransition(to: viewController.navigationController?.view, animation: animation)
    }
    
    viewController.navigationController?.pushViewController(vc, animated: animation == nil)
}

Here is applyTransition:

private func applyTransition(to view: UIView?, animation: AnimationType) {
    guard let view = view else { return }
    let transition = CATransition()
    transition.duration = 0.5
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    switch animation {
        case .push:
            transition.type = CATransitionType.push
            transition.subtype = CATransitionSubtype.fromRight
        case .fade:
            transition.type = CATransitionType.fade
    }
    view.layer.add(transition, forKey: nil)
}