【问题标题】:NavigationBar Background Color导航栏背景颜色
【发布时间】:2020-08-12 19:30:39
【问题描述】:

我正在尝试更改将在导航堆栈中推送的导航栏背景颜色。我在 Tabbar 控制器下使用导航控制器。 当我在更改导航栏颜色后推动视图控制器时,第一次尝试它不起作用。当我通过点击标签栏项目重新加载此视图时,它可以工作。

为什么它在第一次尝试时不起作用?

从另一个 Viewc 控制器调用的视图控制器

func showProjectDetails(indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "MyTaskVC") as! MyTaskVC
    vc.viewMode = .ProjectDetails
    vc.currentProjectName = projects[indexPath.row].projectName
    navigationController?.pushViewController(vc, animated: true)
}

查看推送的控制器

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
let appearance = UINavigationBarAppearance()
        appearance.backgroundColor = .green
        appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

        UINavigationBar.appearance().tintColor = .white
        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().compactAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance

}

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    将此代码添加到您的viewDidLoad()

    override func viewDidLoad() {
            super.viewDidLoad()
    
          if  let navigationBar = navigationController?.navigationBar {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = .green
            appearance.titleTextAttributes = [.foregroundColor: UIColor.red]
            appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    
            let barAppearence = UIBarButtonItemAppearance()
            barAppearence.normal.titleTextAttributes = [.foregroundColor: UIColor.yellow]
    
            appearance.buttonAppearance = barAppearence
    
            navigationBar.scrollEdgeAppearance = appearance
            navigationBar.compactAppearance = appearance
            navigationBar.standardAppearance = appearance
    
            // Do any additional setup after loading the view.
        }
      }
    

    【讨论】:

      【解决方案2】:

      你应该创建一个UINavigationController的子类并自定义它,如果你使用Interface Builder,你可以在Identify Inspector中设置NavigationController自定义类。

          import UIKit
      
          class YourNavigationController: UINavigationController { 
      
              override func viewWillAppear(_ animated: Bool) {
                  super.viewWillAppear(animated)
                  let barAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [YourNavigationController.self])
                  barAppearance.tintColor = UIColor(named: "Blue" 
              } 
      
              override func viewDidLoad() {
                  super.viewDidLoad()                        
              }    
      
            }
      

      您可以阅读更多here

      【讨论】:

        猜你喜欢
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多