4

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

输出

4

2 回答 2

10

这些行完全没有意义:

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
于 2021-09-23T15:15:51.957 回答
-3

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

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

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

于 2021-09-23T13:30:56.520 回答