Search code examples
iosswiftxcodeuinavigationbaruicolor

NavigationBar backgroundColor not coloring statusbar background


I am having some problems coloring the navigationBar. I have tried using different methods from some different tutorials but nothing seems to work the way that I want it to work. I am using the following code in my AppDelegate:

    let navAppeareance = UINavigationBarAppearance()

    navAppeareance.configureWithOpaqueBackground()
    navAppeareance.backgroundColor = .systemRed

    //Setup buttons
    let buttonDone = UIBarButtonItemAppearance(style: .done)
    buttonDone.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    navAppeareance.doneButtonAppearance = buttonDone

    let buttonPlain = UIBarButtonItemAppearance(style: .plain)
    buttonPlain.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    navAppeareance.buttonAppearance = buttonPlain

    //Set appearances
    UINavigationBar.appearance().standardAppearance = navAppeareance
    UINavigationBar.appearance().scrollEdgeAppearance = navAppeareance

    UINavigationBar.appearance().backgroundColor = .systemRed

The above code yields the following result: Result of above code I want the statusbar to be the same color as the navigationBar, but it takes the color of the backgroundColor of the view.


Solution

  • use this in AppDelegate

    let navBarAppearnce = UINavigationBar.appearance()
    navBarAppearnce.barTintColor = UIColor.red // the color you want
    

    Result