iosswiftxcodeuinavigationbaruinavigationbarappearance

I also want to change the background color of the Navigation Bar while it is fixed. How can I do?


I'm developing a notes application and I want the colors of the notes to match the color of the Navigation Bar. When I set the color of the Navigation Bar, the color appears only when I scroll the screen. I want the color to show up without scrolling the screen.

Screenshot without scrolling

enter image description here

Screenshot with scrolling

enter image description here

I use a Xcode 14 and Swift 5 I tried navigationController?.navigationBar.barTintColor = UIColor.flatSkyBlue() AND navigationController?.navigationBar.backgroundColor = UIColor.flatSkyBlue() but it didn't work.

Thank you for help.


Solution

  • Try to configure your navBar like this, in viewDidLoad declare UINavigationBarAppearance and set navigation bar:

    let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.backgroundColor = UIColor.red // set your bg color
        
        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
        
        navigationController?.navigationBar.prefersLargeTitles = true // false if you don't want large title
        navigationController?.navigationBar.tintColor = .white
        navigationItem.title = "title"