如何更改iOS 15导航栏和返回按钮的颜色

10

我有一个 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]
}

Output

2个回答

18

这几行完全没有意义:

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

-5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接