NavigationBar 和 TabBar 背景颜色始终为灰色

NavigationBar and TabBar background colors are always grey

我的 TabBar 和 navigationBar 总是灰色的,尝试了我找到的所有 Whosebug 解决方案,但 none 有效。他们总是保持灰色。
这是我的AppDelegate代码,MenuViewController是一个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()

看看你的这部分代码:

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

您正在将 rootViewController 设置为 UINavigationController 的另一个实例,该实例具有 MainViewController 的实例作为 rootViewController。

如果你想用 MenuViewController 的实例展示你的样式导航控制器,你应该将它设置为 windows 根视图控制器:

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()