Home > Net >  NavigationBar background color not working in swift5
NavigationBar background color not working in swift5

Time:11-21

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

CodePudding user response:

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

  • Related