[英]Why the NavigationBar background color not change?
當用戶選擇不同的 Theme 樣式時,我需要更改 navigationBar 背景顏色。
但奇怪的是,用戶選擇“Dark”模式后,進入backgorund,然后回到前台,如果用戶想改回“Light”模式,導航欄仍然是黑色的,有一個“_UIVisualEffectBackdropView”仍然是黑暗的。
但是如果用戶在進入后台之前選擇了“Light”模式,那么一切正常。
我該如何修復這個錯誤? 下面是代碼和圖片:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case 0:
self.changeToLightColor()
default:
self.changeToDarkColor()
}
}
private func changeToLightColor() {
self.navigationController?.navigationBar.barStyle = .default
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
private func changeToDarkColor() {
self.navigationController?.navigationBar.barStyle = .black
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
非常感謝您的幫助和提前回答!
好吧,花了一點時間來弄清楚如何解決這個問題,解決方案非常簡單。
只需將navigationBar
的barTintColor
設置為您需要的顏色即可。
private func changeToLightColor() {
self.navigationController?.navigationBar.barStyle = .default
//Set to white color
self.navigationController?.navigationBar.barTintColor = UIColor.white
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemBlue]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
private func changeToDarkColor() {
self.navigationController?.navigationBar.barStyle = .black
//Set to black color
self.navigationController?.navigationBar.barTintColor = UIColor.black
let textAttribute = [NSAttributedString.Key.foregroundColor: UIColor.systemGreen]
self.navigationController?.navigationBar.titleTextAttributes = textAttribute
}
我做了之后,問題就解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.