简体   繁体   English

当设备处于暗模式时,将应用程序切换到暗模式 | Swift iOS 13

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

just used appareance for named color in assets like this.只是在这样的资产中使用了命名颜色的外观。 在此处输入图像描述 so when the app open and the dark mode is on all collors would change to their correspondin dark appareance.所以当应用程序打开并且黑暗模式开启时,所有颜色都会变成它们对应的黑暗外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM