How do I transition so that NavigationBar is not shared before and after the screen transition?

Asked 2 years ago, Updated 2 years ago, 102 views

I would appreciate it if you could let me know how to transition without sharing NavigationBar as shown in the image below.

I think I will write the following code for normal screen transition.

navigationController?pushViewController(secondViewController, animated:true)

However, I think NavigationBar will be shared.

Thank you for your cooperation.

Application Image

swift ios xcode navigationbar

2022-09-30 15:43

1 Answers

As far as I know, one Navigation Controller always shares the Navigation Bar, so I think we will create a new Navigation Controller as follows and present it with our own transition animation.

First, implement an animator that conforms to the UIViewControllerAnimatedTransitioning protocol.This animator describes the animation of the transition (the code below also shadows during the transition).

 class animator:NSObject, UIViewControllerAnimatedTransitioning{
    var presented = false

    functionDuration(using transitionContext:UIViewControllerContextTransitioning?) - > TimeInterval {
        return 1.0
    }

    funnelTransition(using transitionContext:UIViewControllerContextTransitioning) {
        let from VC
                 = transitionContext.viewController (forKey:UITtransitionContextViewControllerKey.from)
        let toVC=transitionContext.viewController (forKey: UITransitionContextViewControllerKey.to)

        if presented {
            dispatchTransition (transitionContext:transitionContext, toView:toVC!.view, fromView:fromVC!.view)
        } else{
            presentTransition (transitionContext:transitionContext, toView:toVC!.view, fromView:fromVC!.view)
        }
    }

    func presentTransition(transitionContext:UIViewControllerContextTransitioning, toView:UIView, fromView:UIView){
        let containerView = transitionContext.containerView
        containerView.insertSubview(toView, aboveSubview:fromView)
        toView.frame = toView.frame.offsetBy (dx:containerView.frame.width, dy:0.0)
        addShadow (to:toView)

        UIView.animate(withDuration:transitionDuration(using:transitionContext), delay:0.0, options: .curveEaseInOut, animations:{
            toView.frame = containerView.frame
        }) {finished in
            self.removeShadow (from:toView)
            self.presented=true
            transitionContext.completeTransition(true)
        }
    }

    funcdismissTransition(transitionContext:UIViewControllerContextTransitioning, toView:UIView, fromView:UIView){
        let containerView = transitionContext.containerView
        containerView.insertSubview(toView, belowSubview:fromView)
        toView.frame = containerView.bounds
        addShadow (to:fromView)

        UIView.animate(withDuration:transitionDuration(using:transitionContext), delay:0.0, options: .curveEaseInOut, animations:{
            fromView.frame = fromView.frame.offsetBy (dx:containerView.frame.width, dy:0.0)
        }) {finished in
            self.removeShadow (from:fromView)
            self.presented=false
            transitionContext.completeTransition(true)
        }
    }

    funcaddShadow (to view:UIView) {
        view.layer.shadowColor=UIColor.black.cgColor
        view.layer.shadowOpacity=0.2
        view.layer.shadowOffset = CGSize (width: -5.0, height: 0.0)
        view.layer.shadowPath=UIBezierPath(rect:view.bounds).cgPath
        view.layer.shouldRasterize=true
        view.layer.rasterizationScale=UISscreen.main.scale
    }

    US>func removeShadow (from view:UIView) {
        view.layer.shadowPath=.none
        view.layer.shadowColor=.none
        view.layer.shadowOpacity=0.0
    }
}

Then, match the controller to the UIViewControllerTransitioningDelegate and return the animator.It also implements transition codes.The transition puts the next controller that you want to view on the new Navigation Controller and presents it.(This code describes the transition as IBAction when you tap the button.)

class FirstViewController:UIViewController, UIViewControllerTransitioningDelegate{
    let animator=Animator()

    @ IBAction func buttonTapped(_sender:Any){
        let controller = SecondViewController(nibName:.none, bundle:.none)
        let navController = UINavigationController (rootViewController: controller)
        navController.transitioningDelegate=self
        present(navController, animated: true)
    }

    funvisionController (forPresented present:UIViewController,
                             presenting —UIViewController,
                             source:UIViewController)->UIViewControllerAnimatedTransitioning?{
        return animator
    }

    funvisionController (forDisabled: UIViewController)->UIViewControllerAnimatedTransitioning?{
        return animator
    }
}

Use the dismiss method to return to the original controller from the controller listed above.

dismiss(animated:true)


2022-09-30 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.