skip to Main Content

Recently I updated my Xcode to 13 and after that, I am facing some issues with the navigation bar and Status bar. I am using the tab bar in my view controller. After updating the Xcode, according to the version, I added some code related to the navigation bar.

if #available(iOS 15.0, *) {
     tableView.sectionHeaderTopPadding = 0

     let appearance = UINavigationBarAppearance()
     appearance.configureWithOpaqueBackground()
     appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
     appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

     // Customizing our navigation bar
     navigationController?.navigationBar.tintColor = .white
     navigationController?.navigationBar.barTintColor = .white
     navigationController?.navigationBar.standardAppearance = appearance
     navigationController?.navigationBar.scrollEdgeAppearance = appearance
}

Everything is working when I first opened the app. When I click on the other tab and then this tab. The status bar text color is changing.

enter image description here

enter image description here

I tried different ways to set the status bar text color. But nothing worked for me.

2

Answers


  1. Use this function:

    extension YourViewController {
        override var preferredStatusBarStyle: UIStatusBarStyle {
            .lightContent
        }
    }
    
    Login or Signup to reply.
  2. If you just want to set to white colour you status bar in the entire project you can easily do it by adding the following keys into your project.plist file:

    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UIUserInterfaceStyle</key>
    <string>Light</string>
    

    So, what does these keys do… You are just defining that you status bar colour will not be controlled by the any view controller and that the default colour will be "light theme" that is pretty much the white colour that you want in this case…

    this would be the best approach if you do not intent to update the status bar colour all over the place.

    this was tested and is working as expected on Xcode 13 and iOS [13.x,14.x,15.x]

    If you want to update to customise or update the colours for the navigation bar you would should be able to do with an extension like this one bellow, just few examples but you guys should get what I mean:

    extension UINavigationBar {
    
    func clearBackground() {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithTransparentBackground()
        navBarAppearance.shadowColor = UIColor.clear
        navBarAppearance.titleTextAttributes       = [NSAttributedString.Key.foregroundColor: UIColor.white]
        self.standardAppearance = navBarAppearance
        self.scrollEdgeAppearance = navBarAppearance
    }
    
    func blackColor() {
        // Create an Appearance and customise as you wish.
        // in this case just a black background with titles in white.
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.shadowColor = UIColor.black
        navBarAppearance.titleTextAttributes       = [NSAttributedString.Key.foregroundColor: UIColor.white]
        self.standardAppearance = navBarAppearance
        self.scrollEdgeAppearance = navBarAppearance
    }
    

    }

    with this code you should be able to easy update on any view controller by just calling something like:

    override func viewDidLoad() {
        super.viewDidLoad()
        // set the navigation bar color as transparent  
        self.navigationController?.navigationBar.clearBackground()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search