简体   繁体   中英

NavigationBar background color not working in swift5

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

在此处输入图像描述

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

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:

在此处输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM