UINavigationBar change colors on push

后端 未结 4 1972
长情又很酷
长情又很酷 2020-12-23 17:49

I\'m using 2 different bar tint colors at UINavigationBar in different views. I\'n changing color with that method in both views:

override func          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 18:02

    To achieve this kind of animation you should use UIViewControllerTransitionCoordinator as Apple documentation say it is :

    An object that adopts the UIViewControllerTransitionCoordinator protocol provides support for animations associated with a view controller transition.(...)

    So every UIViewController has own transitionController. To get this you should call in the UIViewControllerClass :

    self.transitionCoordinator()

    From documentation:

    Returns the active transition coordinator object.

    So to get the result that you want you should implement animateAlongsideTransition method in viewController transitionCoordinatior. Animation works when you click backButton and swipe to back.

    Example :

    First Controller :

    class ViewControllerA: UIViewController {
    
        override func loadView() {
            super.loadView()
            title = "A"
            view.backgroundColor = .white
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(self.showController))
            setColors()
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            animate()
        }
    
        func showController() {
            navigationController?.pushViewController(ViewControllerB(), animated: true)
        }
    
        private func animate() {
            guard let coordinator = self.transitionCoordinator else {
                return
            }
    
            coordinator.animate(alongsideTransition: {
                [weak self] context in
                self?.setColors()
            }, completion: nil)
        }
    
        private func setColors() {
            navigationController?.navigationBar.tintColor = .black
            navigationController?.navigationBar.barTintColor = .red
        }
    }
    

    Second Controller:

    class ViewControllerB : UIViewController {
    
        override func loadView() {
            super.loadView()
            title = "B"
            view.backgroundColor = .white
            setColors()
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            animate()
        }
    
        override func willMove(toParentViewController parent: UIViewController?) { // tricky part in iOS 10
            navigationController?.navigationBar.barTintColor = .red //previous color
            super.willMove(toParentViewController: parent)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            navigationController?.navigationBar.barTintColor = .blue
        }
    
        private func animate() {
            guard let coordinator = self.transitionCoordinator else {
                return
            }
            coordinator.animate(alongsideTransition: {
                [weak self] context in
                self?.setColors()
            }, completion: nil)
        }
    
        private func setColors(){
            navigationController?.navigationBar.tintColor = .black
            navigationController?.navigationBar.barTintColor = .blue
        }
    
    }
    

    UPDATE iOS 10

    In the iOS 10 the tricky part is to add the willMoveTo(parentViewController parent: UIViewController?) in the second ViewController. And set the navigationBar tintColor to the color value of previous controller. Also, in viewDidAppear method in second ViewControler set the navigationBar.tintColor to the color from second viewController.

    Check out my example project on github

提交回复
热议问题