我们如何在 iOS 13 的当前 iOS 应用中支持暗模式?

Pus*_*shp 5 iphone ios ios13 ios-darkmode

我当前的应用程序是用 objC 和 Swift 开发的。我需要支持暗模式。谁能建议我如何在全球范围内实现这一目标?

小智 6

这是用于添加颜色逻辑的代码应该出现在暗模式下。

if self.traitCollection.userInterfaceStyle == .dark {

  //Add your Dark mode colors here
 } else {

  //Your normal colors should appear here
 }
Run Code Online (Sandbox Code Playgroud)

要了解有关在 iOS 应用程序中调整暗模式的更多信息,请参阅以下博客文章。

如何在您的 iOS 应用中采用 iOS 13 暗模式


Cra*_*007 5

Xcode11-beta 的界面构建器中提供了几种语义系统颜色。请使用它们来支持 .light 和 .darkMode

请按照基本说明逐步操作。

  1. UIView - 使用界面构建器中的 systemBackgroundColor。
  2. UILabel - 使用界面构建器中的 defaulLabel 颜色。
  3. CustomView - 请使用 .xcassests 目录并创建新的颜色集,从属性检查器中为 .light 和 .darkMode添加外观选项,并为两种模式提供不同的颜色。
  4. CustomImages - 请使用 .xcassests 目录并从属性检查器中为 .light 和 .darkMode添加外观选项,并为两种模式提供不同的图像。
  5. 还有一个选项可以使用代码提供不同的颜色和不同的图像。

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        updateUIForiOS13()
    }
    
    private func isDarkMode() -> Bool{
        if #available(iOS 12.0, *) {
            let isDark = traitCollection.userInterfaceStyle == .dark ? true : false
            return isDark
        }
        return false
    }
    
    Run Code Online (Sandbox Code Playgroud)