如何在Swift 5 中以编程方式更改导航栏的背景颜色?

How to change the background color of navigation bar programmatically in Swift 5?

我正在使用 Swift 5 并尝试在执行弹出操作时更改导航栏颜色。我使用了下面编写的代码,但它对我不起作用

func changeRootToLogin()
{
    guard let rootVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OnboardingViewController") as? OnboardingViewController else {
        return
    }

    let navigationController = UINavigationController(rootViewController: rootVC)
    self.navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.2933186293, green: 0.8084665537, blue: 0.8478894234, alpha: 1)
    self.navigationController?.navigationBar.isHidden = false
    UIApplication.shared.windows.first?.rootViewController = navigationController
    UIApplication.shared.windows.first?.makeKeyAndVisible()
}

使用这个

self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 
0.2933186293, green: 0.8084665537, blue: 0.8478894234, alpha: 1)