[英]How can I reload views after choose dark mode?
我正在使用這個 pod 為我的應用程序添加黑暗模式。 https://github.com/draveness/NightNight
當我再次重新啟動應用程序時它運行良好,但我想更改應用程序內部的主題。 因此,我將 UISwitch 添加到我的側面板中,以便用戶可以更改主題。
我為它添加了這個代碼,有些顏色變化很好,但有些顏色不影響。 例如 NavigationBar 背景顏色變化良好,但標題顏色沒有變化。
UISwitch 動作:
@IBAction func switchMode(_ sender: UISwitch) {
if sender.isOn {
switcher.isOn = true
NightNight.theme = NightNight.Theme.night
UITabBar.appearance().barTintColor = UIColor(hexString: "#141d27")
UITabBar.appearance().isTranslucent = true
UITabBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
UINavigationBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().barTintColor = UIColor(hexString: "#141d27")
for window in UIApplication.shared.windows {
for view in window.subviews {
view.removeFromSuperview()
window.addSubview(view)
}
}
UserDefaults.standard.set("night", forKey: "colormode")
} else {
switcher.isOn = false
NightNight.theme = NightNight.Theme.normal
UITabBar.appearance().barTintColor = UIColor.white
UITabBar.appearance().isTranslucent = true
UITabBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
UINavigationBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().barTintColor = UIColor.white
for window in UIApplication.shared.windows {
for view in window.subviews {
view.removeFromSuperview()
window.addSubview(view)
}
}
UserDefaults.standard.set("normal", forKey: "colormode")
}
}
通常灰色文本顏色(用戶名和導航標題)在淺色模式下必須為黑色,但它們不會改變。
當view mode
改變時使用回調。 按預期更改顏色。
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// do whatever you want to do
}
查看 RxSwift 和 RxCocoa。 他們都可以在這里找到。 它們是 Swift 中反應式編程的反應式框架。 您可以根據需要創建 observable 以被動地更改視圖的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.