[英]Issue switching app to Dark Mode when device is in dark mode | Swift iOS 13
When dark mode is already enabled on the device and I open my app all of the color sets that specify dark mode are not being applied:当设备上已经启用暗模式并且我打开我的应用程序时,所有指定暗模式的颜色集都没有被应用:
But when having the app already open and switching to Dark Mode from Control Center, the following code changes the color:但是当应用程序已经打开并从控制中心切换到暗模式时,以下代码会更改颜色:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard let previousTraitCollection = previousTraitCollection else {return}
if #available(iOS 13.0, *) {
if previousTraitCollection.hasDifferentColorAppearance(comparedTo: traitCollection) {
//Dark Mode
let inputBackground = UIColor(named: "TEST1backgroundColor")
let inputPlaceholder = UIColor(named: "TEST1placeholderColor")
passwordBack.layer.backgroundColor = inputBackground?.cgColor
TextField1.placeholderColor = inputPlaceholder!
TextField2.placeholderColor = inputPlaceholder!
TextField1.textColor = inputPlaceholder!
TextField2.textColor = inputPlaceholder!
}
}
}
How can have this color change be applied when starting an already in Dark Mode?启动已经处于暗模式时如何应用这种颜色变化?
CGColors
don't react to dark or light mode. CGColors
不会对深色或浅色模式做出反应。 You don't show the declaration of passwordBack
, but assuming it's a UIView
or a subclass, set its backgroundColor
property directly to inputBackground
instead of using layer.backgroundColor
您没有显示
passwordBack
的声明,但假设它是UIView
或子类,请将其backgroundColor
属性直接设置为inputBackground
而不是使用layer.backgroundColor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.