Search code examples
swiftuinavigationbar

How to change the background color of navigationBar


I am trying to change the background color of the navigation bar to black but without success. I have checked the answer to this related question but none of the solutions have worked. This is what I have tried:

navigationController?.navigationBar.backgroundColor = UIColor.black
UINavigationBar.appearance().backgroundColor = UIColor.black

Solution

  • You can also change the navigation bar color for a specific view controller, this way:

    extension UIViewController {
        func setCustomNavigationColor(color: UIColor = .black, isTranslucent: Bool = false ){
            self.navigationController?.navigationBar.barTintColor = color
            self.navigationController?.navigationBar.isTranslucent = isTranslucent
        }
    }
    

    call this from viewDidLoad()

    setCustomNavigationColor()