【问题标题】:How to change navigation bar & back button colour iOS 15如何更改导航栏和后退按钮颜色 iOS 15
【发布时间】:2021-11-16 16:45:33
【问题描述】:

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

【问题讨论】:

    标签: swift uikit uinavigationbar ios15 uinavigationbarappearance


    【解决方案1】:

    这些行完全没有意义:

    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
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-17
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多