我正在尝试更改将在导航堆栈中推送的Navbar背景色。我在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
}
将此代码添加到您的 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.
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句