美文网首页
iOS 系统导航与自定义导航衔接与动画

iOS 系统导航与自定义导航衔接与动画

作者: 秋叶红90 | 来源:发表于2021-05-26 11:33 被阅读0次

    1首先写一个BaseNavigationController

    import UIKit
    
    class BaseNavigationController: UINavigationController, UIGestureRecognizerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            self.interactivePopGestureRecognizer?.delegate = self
        }
        
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
                if self.viewControllers.count == 1{
                    return false;
                }
                return true;
        }
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    

    2 写一个父类

    import UIKit
    
    class YQBaseViewController: UIViewController {
        func isHiddenNav() -> Bool {
            return false
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated);
    //            self.navigationController?.setNavigationBarHidden(true, animated: true)
            self.navigationController?.setNavigationBarHidden(self.isHiddenNav(), animated: animated)
    
        }
        
    //    override func viewWillDisappear(_ animated: Bool) {
    //        super.viewWillDisappear(animated);
    //        self.navigationController?.navigationBar.isHidden = false;
    //    }
      
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    
    

    2 FirstVC

    
    class FirstViewController: YQBaseViewController {
        override func isHiddenNav() -> Bool {
            return true
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
        @IBAction func btnClick(_ sender: Any) {
            let toVC = SecondViewController.init()
            
            self.navigationController?.pushViewController(toVC, animated: true)
        }
        
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    

    3 Second

    import UIKit
    
    class SecondViewController: YQBaseViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            self.title = "系统导航"
        }
    
    
        /*
        // MARK: - Navigation
    
        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */
    
    }
    
    

    ViewController

    import UIKit
    
    class ViewController: YQBaseViewController {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
    
            self.title = "系统导航"
        }
    
        @IBAction func tbClick(_ sender: Any?) {
            
            let toVC = FirstViewController.init()
            self.navigationController?.pushViewController(toVC, animated: true)
            
        }
    }
    
    
    

    Simulator Screen Recording - iPhone 12 - 2021-05-26 at 11.26.24.gif

    效果如图

    相关文章

      网友评论

          本文标题:iOS 系统导航与自定义导航衔接与动画

          本文链接:https://www.haomeiwen.com/subject/fzhksltx.html