iOS 11.x系统颜色

问题描述 投票:0回答:1

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

ios swift colors controls
1个回答
4
投票

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

iOS system colors from the hig

Colors in the Human Interface Guidelines

© www.soinside.com 2019 - 2024. All rights reserved.