iOS 11.x system colors

ios swift colors controls

2089 观看

2回复

218 作者的声誉

I've read a lot of posts on how to customize Views colors, but nothing about retrieving system colors for standard controls like the Navigation Bar, Status Bar and Tab Bar in iOS 11.x or previous. The class UIColor has 3 system colors but they are pretty useless. Calling UINavigationBar.appearance(), for example, is of little help because it'll probably return the "clear" color for the default light color scheme, in case nothing has been defined in the application plist. So why doesn't Apple supply a way to programmatically get the system colors as others do (for Windows and Android) ? Has anybody any idea where to find them ? Tx in advance.

作者: Sergiob 的来源 发布者: 2017 年 12 月 27 日

回应 2


5

7628 作者的声誉

iOS does not provide a way to access these colors programmatically.

Instead, it's simple enough to add these colors to your own project. If the colors change in a future iOS version (and you want to use those new colors instead), you will need to update your app.

In most cases, this is not an issue since apps define their own colors for branding purposes.

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)
        }
    }

}

Sample usage:

myView.backgroundColor = SystemColor.blue.uiColor

If you prefer, these colors could also be defined as an extension on UIColor like so:

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

and the usage would look like this:

UIColor.systemBlue

iOS system colors from the hig

Colors in the Human Interface Guidelines

作者: nathangitter 发布者: 2017 年 12 月 27 日

0

2170 作者的声誉

Since Xcode 11.0 you can use UIColor.systemRed, UIColor.systemGreen, UIColor.systemBlue and other UIColor.system* properties to solve the issue you described.

作者: Roman Podymov 发布者: 2019 年 8 月 2 日
32x32