我在我的应用程序中创建自定义元素,并希望与新iOS的外观和感觉相匹配。iOS 7向我们介绍了一种非常常见的浅蓝色,这是几个元素的默认颜色或色调,包括系统按钮、分段控件等。他们很容易使用IB选择颜色,如下所示:

但是,我还没有找到如何通过编程轻松地访问颜色。我检查了UIColor文档,在类本身中似乎没有任何用于蓝色系统颜色的访问器。

我的问题是:这种颜色有简单的访问器吗?[UIColor ?]之类的东西?如果不是,有人知道这种颜色的确切RGB值吗?


当前回答

本机扩展与预定义的系统颜色提供了你正在寻找的:

// System colors

extension UIColor {

   
    /* Some colors that are used by system elements and applications.
     * These return named colors whose values may vary between different contexts and releases.
     * Do not make assumptions about the color spaces or actual colors used.
     */
    
    ... 

    @available(iOS 7.0, *)
    open class var systemBlue: UIColor { get }
    ... 
}

你可以直接使用它:

myView.tintColor = .systemBlue

其他回答

本机扩展与预定义的系统颜色提供了你正在寻找的:

// System colors

extension UIColor {

   
    /* Some colors that are used by system elements and applications.
     * These return named colors whose values may vary between different contexts and releases.
     * Do not make assumptions about the color spaces or actual colors used.
     */
    
    ... 

    @available(iOS 7.0, *)
    open class var systemBlue: UIColor { get }
    ... 
}

你可以直接使用它:

myView.tintColor = .systemBlue

iOS 7默认的蓝色是R:0.0 G:122.0 B:255.0

UIColor *ios7BlueColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];

在很多情况下,你所需要的只是

[self tintColor] 
// or if in a ViewController
[self.view tintColor]

或者是swift

self.tintColor
// or if in a ViewController
self.view.tintColor

从iOS 7有一个API,你可以获得(并设置)色调颜色:

self.view.tintColor

或者如果你需要CGColor:

self.view.tintColor.CGColor

它似乎是[UIColor colorWithRed:0.0绿色:122.0/255.0蓝色:1.0 alpha:1.0]。