如何以编程方式获得 iOS 7 默认蓝色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19032940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:44:27  来源:igfitidea点击:

How can I get the iOS 7 default blue color programmatically?

ioscolorsios7uicolor

提问by Joel H.

I'm creating custom elements in my app and want to match the look and feel of the new iOS. iOS 7 introduced to us a very common lighter blue color, the default color or tint for several elements, including the system button, segmented control, etc. They've made it easy to select the color using IB, as seen here:

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

enter image description here

在此处输入图片说明

However, I haven't found how to easily access the color programmatically. I checked out the UIColor documentation, and there doesn't seem to be any accessor for the blue system color in the class itself.

但是,我还没有找到如何以编程方式轻松访问颜色。我查看了UIColor 文档,并且在类本身中似乎没有任何蓝色系统颜色的访问器。

Here's my question: does a simple accessor exist for this color? [UIColor ?]or something like it? If not, does someone know the exact RGB valuesfor that color?

这是我的问题:这种颜色是否存在简单的存取器?[UIColor ?]或类似的东西?如果没有,有人知道该颜色的确切RGB 值吗?

回答by Aaron Brager

Use self.view.tintColorfrom a view controller, or self.tintColorfrom a UIViewsubclass.

使用self.view.tintColor从一个视图控制器,或self.tintColor从一个UIView子类。

回答by Greg

It appears to be [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0].

看来是[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]

screenshot showing Colors window

显示颜色窗口的屏幕截图

回答by tarum

iOS 7 default blue color is R:0.0 G:122.0 B:255.0

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];

回答by Scott Carter

According to the documentation for UIButton:

根据 UIButton 的文档:

In iOS v7.0, all subclasses of UIView derive their behavior for tintColor from the base class. See the discussion of tintColor at the UIView level for more information.

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

Assuming you don't change the tintColor before grabbing the default value, you can use:

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

self.view.tintColor

回答by Rick

Here is a simple method to get the default system tint color:

这是获取默认系统色调颜色的简单方法:

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

回答by Ego Slayer

Hex Color code

十六进制颜色代码

#007AFF

and you need this libary https://github.com/thii/SwiftHEXColors

你需要这个库 https://github.com/thii/SwiftHEXColors

ps. iOS, Swift

附:iOS,斯威夫特

回答by Dmitry Kozlov

swift 4 way:

快速 4 种方式:

extension UIColor {
  static let system = UIView().tintColor!
}

回答by Stanislau Baranouski

This extension gives you native system blue color.

此扩展程序为您提供本机系统蓝色。

extension UIColor {

    static var systemBlue: UIColor {
        return UIButton(type: .system).tintColor
    }

}

UPDATE

更新

Please forget what I wrote above, just figured out - there're native extension with predefined system colors we've been looking for, including system blue:

请忘记我上面写的内容,只是想通了 - 我们一直在寻找带有预定义系统颜色的本机扩展,包括系统蓝色

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

You can use it directly:

您可以直接使用它:

myView.tintColor = .systemBlue

回答by Lukasz Czerwinski

Get the color automatically by using this code:

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

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

回答by cscott530

The UIWindow.tintColormethod wasn't working for me in iOS8 (it was still black), so I had to do this:

UIWindow.tintColor方法在 iOS8 中对我不起作用(它仍然是黑色的),所以我不得不这样做:

let b = UIButton.buttonWithType(UIButtonType.System) as UIButton
var color = b.titleColorForState(.Normal)

This gave the proper blue tint seen in a UIBarButtonItem

这给出了在 a 中看到的适当的蓝色色调 UIBarButtonItem