我使用此 pod 为我的应用程序添加深色模式。 https://github.com/draveness/NightNight
当我再次重新启动应用程序时它运行良好,但我想更改应用程序内部的主题。因此,我将 UISwitch 添加到我的侧面板,以便用户可以更改主题。
我为它添加了这段代码,有些颜色变化很好,但有些颜色不影响。例如 NavigationBar 背景颜色变化良好,但标题颜色没有变化。
UISwitch Action :
@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")
}
}
通常灰色文本颜色(用户名和导航标题)在浅色模式下必须为黑色,但它们没有改变。
最佳答案
查看模式
更改时使用回调。
按预期更改颜色。
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// do whatever you want to do
}
关于ios - 选择深色模式后如何重新加载 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989539/