Enamul Haque
Enamul Haque

Reputation: 5053

NavigationBar background color not working in swift5

I am using NavigationBar in swift5. NavigationBar background color shows black color.. here is my image

enter image description here

Here is my code:

    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.navigationController?.navigationBar.tintColor = UIColor.red
            self.navigationController?.navigationBar.barTintColor = UIColor.green
            self.navigationController?.navigationBar.barTintColor = .red
            self.navigationItem.title = "ABC title"
            self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
            self.navigationController?.navigationBar.isTranslucent = false
        }
    }

What is the problem of my code? Please help me

Upvotes: 2

Views: 137

Answers (2)

Rahul John
Rahul John

Reputation: 498

You can simply change the navigation bar background Colour using this Code

self.navigationController?.navigationBar.backgroundColor = UIColor.green

Upvotes: 1

AntonioWar
AntonioWar

Reputation: 337

This is why in iOS 15 NavigationBars use the scrollEdgeAppearance, if you want to use old appearance you have to declare it like this :

override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = .red
        appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        self.navigationController?.navigationBar.standardAppearance = appearance;
        self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance
        
        self.navigationItem.title = "ABC title"
    }

That's the result :

enter image description here

Upvotes: 3

Related Questions