UINavigationBar change colors on push

后端 未结 4 1969
长情又很酷
长情又很酷 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:12

    I've coded final solution that looks most comfortable to use (don't need to use a lot of overrides in own view controllers). It works perfectly at iOS 10 and easy adoptable for own purposes.

    GitHub

    You can check GitHub Gist for full class code and more detailed guide, I won't post full code here because Stackoverflow is not intended for storing a lot of code.

    Usage

    Download Swift file for GitHub. To make it work just use ColorableNavigationController instead of UINavigationController and adopt needed child view controllers to NavigationBarColorable protocol.

    Example:

    class ViewControllerA: UIViewController, NavigationBarColorable {
        public var navigationBarTintColor: UIColor? { return UIColor.blue }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Push", style: .plain, target: self, action: #selector(self.showController))
        }
    
        func showController() {
            navigationController?.pushViewController(ViewControllerB(), animated: true)
        }
    }
    
    class ViewControllerB: UIViewController, NavigationBarColorable {
        public var navigationBarTintColor: UIColor? { return UIColor.red }
    }
    
    let navigationController = ColorableNavigationController(rootViewController: ViewControllerA())
    

提交回复
热议问题