I am trying to change Navbar background color that will be push in navigation stack. I am using navigation controller under Tabbar controller. When I push view controller after changing the navbar color, in first attempt it does not work. 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
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()
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.
}
}