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

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

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


当前回答

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

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

其他回答

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

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

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

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

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

或者是swift

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

从视图控制器中使用self。view. tintcolor,或者self。tintColor来自UIView子类。

使用下面的代码自动获取颜色:

static let DefaultButtonColor = UIButton(type: UIButtonType.System).titleColorForState(.Normal)!