如何更改导航栏和后退按钮颜色 iOS 15

Dil*_*lan 10 uinavigationbar uikit swift uinavigationbarappearance ios15

我有UIkit一个项目,我想更改导航栏颜色和后退按钮颜色。它在以前的版本上工作正常。但在 iOS 15 中则不然。我在上面添加了以下代码AppDelegate,它更改了标题颜色但不更改后退按钮项目颜色。如何修复它?

if #available(iOS 15.0, *) {
   let appearance = UINavigationBarAppearance()
   let navigationBar = UINavigationBar()
   appearance.configureWithOpaqueBackground()
   appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
   appearance.backgroundColor = .red
   navigationBar.tintColor = .white
   navigationBar.standardAppearance = appearance;
   UINavigationBar.appearance().scrollEdgeAppearance = appearance
}else{
   let navBarAppearnce = UINavigationBar.appearance()
   navBarAppearnce.tintColor = .white
   navBarAppearnce.barTintColor = .red
   navBarAppearnce.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
}
Run Code Online (Sandbox Code Playgroud)

输出

mat*_*att 18

这些行完全没有意义:

let navigationBar = UINavigationBar()
navigationBar.tintColor = .white
navigationBar.standardAppearance = appearance
Run Code Online (Sandbox Code Playgroud)

您正在创建一个导航栏,对其进行配置,然后将其丢弃。这对你的应用程序没有任何作用。有意义地重写:

    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.backgroundColor = .red
    let proxy = UINavigationBar.appearance()
    proxy.tintColor = .white
    proxy.standardAppearance = appearance
    proxy.scrollEdgeAppearance = appearance
Run Code Online (Sandbox Code Playgroud)