[英]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.