Reputation: 998
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.
Upvotes: 3
Views: 10814
Reputation: 9777
Update for 2019
UIColor
now provides static properties to get the system colors.
Example:
UIColor.systemBlue
For a full list of system colors, check out the "Standard Colors" page in the UIKit documentation: https://developer.apple.com/documentation/uikit/uicolor/standard_colors
Old Answer
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
Colors in the Human Interface Guidelines
Upvotes: 14
Reputation: 4521
Since Xcode 11.0 you can use UIColor.systemRed
, UIColor.systemGreen
, UIColor.systemBlue
and other UIColor.system*
properties to solve the issue you described.
Upvotes: 3