[英]How can I reload views after choose dark mode?
Im using this pod for add dark mode to my app.我正在使用这个 pod 为我的应用程序添加黑暗模式。 https://github.com/draveness/NightNight https://github.com/draveness/NightNight
Its working well when I restart app again but I want to change theme inside of app.当我再次重新启动应用程序时它运行良好,但我想更改应用程序内部的主题。 So, I added UISwitch to my sidepanel for user can change theme.因此,我将 UISwitch 添加到我的侧面板中,以便用户可以更改主题。
I added this codes for it and some colors changing well but some colors does not affect.我为它添加了这个代码,有些颜色变化很好,但有些颜色不影响。 For example NavigationBar background color is changing well but title color is not changing.例如 NavigationBar 背景颜色变化良好,但标题颜色没有变化。
UISwitch Action: 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")
}
}
Normally gray text colors(User`s names and navigation title) must to be black in light mode, but they does not changed.通常灰色文本颜色(用户名和导航标题)在浅色模式下必须为黑色,但它们不会改变。
Use callback when view mode
changes.当view mode
改变时使用回调。 Change your colors as expected.按预期更改颜色。
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// do whatever you want to do
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.