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

How to change navigation bar & back button colour iOS 15

我有 UIkit 项目,我想更改导航栏颜色和后退按钮 colour.It 在以前的版本上工作正常。但不在 iOS 15. 我将以下代码放在 AppDelegate 上,它是更改标题颜色而不是后退按钮项目 colour.How 来修复它?

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]
}

在视图控制器的初始化中添加以下代码:

if #available(iOS 14.0, *) {
    navigationItem.backButtonDisplayMode = .minimal
}

这里还有一篇有用的文章: https://sarunw.com/posts/new-way-to-manage-back-button-title-in-ios14/

这些行完全没有意义:

let navigationBar = UINavigationBar()
navigationBar.tintColor = .white
navigationBar.standardAppearance = appearance

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

    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