【问题标题】:iOS13: How to specify color for elevated user interface level in the asset catalogiOS13:如何在资产目录中为提升的用户界面级别指定颜色
【发布时间】:2019-09-20 10:49:14
【问题描述】:

iOS 13 为我们带来了UIUserInterfaceLevel,它可以是.base.elevated。当在暗模式下使用升高的级别时,系统似乎会自动调整提供给UIView 的颜色。

但是,似乎没有办法在资产目录中手动指定.elevated颜色,或者是吗?

唯一的方法似乎是通过the new UIColor constructor

UIColor.init { (traits) -> UIColor in
   traits.userInterfaceLevel == .elevated ? UIColor(named: "myColor-elevated")! : UIColor(named: "myColor")!
}

【问题讨论】:

    标签: uicolor ios13 uitraitcollection ios-darkmode


    【解决方案1】:

    据我所知,颜色资源无法做到这一点。

    当您使用系统背景和填充颜色时,iOS 会在提升级别时自动选择“下一个更高”颜色,即.systemBackground 变为 .secondarySystemBackground 等。

    【讨论】:

    • 我要解决的问题是,当我使用 .tertiarySystemBackground 作为单元格的背景和 .opaqueSeparator 作为分隔符的组合时,这两者在 .elevated 模式下的对比太微妙了,不可能注意分隔符。
    【解决方案2】:

    我的解决方案是将.xcassets 与这个依赖于特征的初始化程序结合起来。对于每种颜色,我创建了一个包含浅色和深色的颜色集和另一个仅包含提升颜色的颜色集(对于任何变体)。

    然后我向UIColor 添加了一个扩展名。此扩展程序提供对整个应用程序所有颜色的轻松访问,并包含选择正确颜色的逻辑。

    extension UIColor {
        static let backgroundPrimary = UIColor { (trait) -> UIColor in
            switch (trait.userInterfaceStyle, trait.userInterfaceLevel) {
            case (.dark, .elevated):
                // For this color set you can set "Appearances" to "none"
                return UIColor(named: "backgroundPrimaryElevated") ?? .black
            default:
                // This color set has light and dark colors specified
                return UIColor(named: "backgroundPrimary") ?? .black
            }
        }
        // ... All the other colors
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2019-11-11
      • 2017-05-30
      • 2015-02-03
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多