iOS 13 黑暗模式

iOS适配暗黑模式-Swift

2020-08-24  本文已影响0人  v尽栗而为v

代码适配

 UITraitCollection.current.userInterfaceStyle
 if UITraitCollection.current.userInterfaceStyle == .dark {
      //暗黑模式
} else {
      //其他模式
}
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        if #available(iOS 13.0, *) {
//是否改变
            if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
                //不同
                if self.currentStyle == false {
                    self.view.backgroundColor = .black
                    self.lbl?.text = "Dark"
                    self.lbl?.textColor = .white
                } else {
                    self.view.backgroundColor = .white
                    self.lbl?.text = "Light"
                    self.lbl?.textColor = .black
                }
                self.currentStyle = !self.currentStyle
            }
        } else {
            // Fallback on earlier versions
        }
    }

图片适配

  1. 在Assets.xcassets中,点击图片-右边第四个(command+option+4快捷键),里面有个Appearance,选择Any,Dark,最后将暗黑模式所需要的图片拖进去即可。


    image.png
上一篇下一篇

猜你喜欢

热点阅读