我正在尝试更改将在导航堆栈中推送的导航栏背景颜色。我在 Tabbar Controller 下使用导航 Controller 。
当我在更改导航栏颜色后插入 View Controller 时,第一次尝试它不起作用。当我通过点击标签栏项目重新加载此 View 时,它可以工作。
为什么它在第一次尝试时不起作用?
从另一个 Viewc Controller 调用的 View 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)
}
查看推送的 Controller
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.
}
}
关于ios - 导航栏背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489457/