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

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

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


当前回答

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

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

或者是swift

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

其他回答

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

// 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

在设置颜色时,您可以像这样设置颜色

[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0]

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

self.view.tintColor

或者如果你需要CGColor:

self.view.tintColor.CGColor

根据UIButton的文档:

在iOS v7.0中,UIView的所有子类都从基类派生tintColor的行为。更多信息请参见UIView级别的tintColor讨论。

假设你在获取默认值之前不改变tintColor,你可以使用:

self.view.tintColor

下面是一个简单的方法来获得默认的系统色调颜色:

+ (UIColor*)defaultSystemTintColor
{
   static UIColor* systemTintColor = nil;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
      UIView* view = [[UIView alloc] init];
      systemTintColor = view.tintColor;
   });
   return systemTintColor;
}