【问题标题】:iOS 11.x system colorsiOS 11.x 系统颜色
【发布时间】:2017-12-27 16:35:24
【问题描述】:

我已经阅读了很多关于如何自定义视图颜色的帖子,但没有关于在 iOS 11.x 或更早版本中检索标准控件(如导航栏、状态栏和标签栏)的系统颜色。 UIColor 类有 3 种系统颜色,但它们毫无用处。例如,调用 UINavigationBar.appearance() 没有什么帮助,因为它可能会返回默认浅色方案的“清晰”颜色,以防应用程序 plist 中没有定义任何内容。那么为什么 Apple 不提供一种方法来像其他人一样(对于 Windows 和 Android)以编程方式获取系统颜色?有人知道在哪里可以找到它们吗?提前发送。

【问题讨论】:

标签: ios swift colors controls


【解决方案1】:

2019 年更新

UIColor 现在提供静态属性来获取系统颜色。

例子:

UIColor.systemBlue

有关系统颜色的完整列表,请查看 UIKit 文档中的“标准颜色”页面:https://developer.apple.com/documentation/uikit/uicolor/standard_colors


旧答案

iOS 不提供以编程方式访问这些颜色的方法。

相反,将这些颜色添加到您自己的项目中非常简单。如果在未来的 iOS 版本中颜色发生变化(并且您想改用这些新颜色),您将需要更新您的应用。

在大多数情况下,这不是问题,因为应用会定义自己的颜色以用于品牌推广。

enum SystemColor {

    case red
    case orange
    case yellow
    case green
    case tealBlue
    case blue
    case purple
    case pink

    var uiColor: UIColor {
        switch self {
        case .red:
            return UIColor(red: 255/255, green: 59/255, blue: 48/255, alpha: 1)
        case .orange:
            return UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
        case .yellow:
            return UIColor(red: 255/255, green: 204/255, blue: 0/255, alpha: 1)
        case .green:
            return UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
        case .tealBlue:
            return UIColor(red: 90/255, green: 200/255, blue: 250/255, alpha: 1)
        case .blue:
            return UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1)
        case .purple:
            return UIColor(red: 88/255, green: 86/255, blue: 214/255, alpha: 1)
        case .pink:
            return UIColor(red: 255/255, green: 45/255, blue: 85/255, alpha: 1)
        }
    }

}

示例用法:

myView.backgroundColor = SystemColor.blue.uiColor

如果您愿意,这些颜色也可以定义为 UIColor 的扩展,如下所示:

extension UIColor {
    static let systemBlue = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1)
    // etc
}

用法如下:

UIColor.systemBlue

Colors in the Human Interface Guidelines

【讨论】:

  • 谢谢@nathan 我知道几种方法可以得到这个结果。我的应用没有自定义颜色。这取决于设计的系统颜色。所以,你的解决方案不能满足我的需求。无论如何,谢谢你的时间
  • @Sergiob 据我所知,没有解决方案可以满足您的需求。在 iOS API 的当前限制下,这是您能做的最好的事情。
  • 投了反对票,因为使用新版本的 Xcode 您可以访问系统颜色。请参阅 Roman Podymov 的帖子。
  • 它在界面指南中明确表示不要这样做,因为颜色值在不同的情况下会发生变化,例如由于深色外观和辅助功能设置。
  • 感谢您指出这一点。更新了答案以在 UIKit 中包含新的 API。
【解决方案2】:

从 Xcode 11.0 开始,您可以使用UIColor.systemRedUIColor.systemGreenUIColor.systemBlue 和其他UIColor.system* 属性来解决您描述的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2019-12-13
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多