简体   繁体   English

导航栏背景颜色

[英]NavigationBar Background Color

I am trying to change Navbar background color that will be push in navigation stack.我正在尝试更改将在导航堆栈中推送的导航栏背景颜色。 I am using navigation controller under Tabbar controller.我在标签栏 controller 下使用导航 controller。 When I push view controller after changing the navbar color, in first attempt it does not work.当我在更改导航栏颜色后推送视图 controller 时,第一次尝试它不起作用。 when i reload this view by tapping tabbar item it works.当我通过点击标签栏项目重新加载此视图时,它可以工作。

Why it is not working in first attempt?为什么它在第一次尝试时不起作用?

view controller called from another Viewc controller查看 controller 从另一个 Viewc controller 调用

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

view conroller that pushed查看推送的控制器

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

}

Add this code in your viewDidLoad()将此代码添加到您的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.
    }
  }

在此处输入图像描述

You should create a subclass of UINavigationController and customize it, if you are using Interface Builder, you can set the NavigationController custom class in the Identify Inspector.您应该创建 UINavigationController 的子类并对其进行自定义,如果您使用的是 Interface Builder,则可以在识别检查器中设置 NavigationController 自定义 class。

    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()                        
        }    

      }

在此处输入图像描述

You can read more here你可以在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM