Search code examples
iosswiftuinavigationcontrolleruitabbarcontroller

NavigationBar and TabBar background colors are always grey


My TabBar and navigationBar are always grey, tried all StackOverflow solutions I found, but none works. They always stay grey.
This is my AppDelegate code, MenuViewController is a TabBarViewController.

self.window = UIWindow(frame: UIScreen.main.bounds)
        
        let menuVC = MenuViewController()
        let navigationController = UINavigationController(rootViewController: menuVC)
        
        navigationController.navigationBar.tintColor = .white
        navigationController.navigationBar.barTintColor = .red
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UINavigationController(rootViewController: MainViewController())
        window?.makeKeyAndVisible()

Solution

  • Take a look at this part of your code:

    window?.rootViewController = UINavigationController(rootViewController: MainViewController())
    

    You're setting the rootViewController to a different instance of UINavigationController that has an instance of MainViewController as rootViewController.

    If you want to present your styled navigation controller with an instance of MenuViewController you should set it as the windows root view controller:

    let menuVC = MenuViewController()
    let navigationController = UINavigationController(rootViewController: menuVC)
            
    navigationController.navigationBar.tintColor = .white
    navigationController.navigationBar.barTintColor = .red
            
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()