228

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:

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.

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 values for that color?

Joel H.
  • 2,764
  • 3
  • 22
  • 34

15 Answers15

256

Use self.view.tintColor from a view controller, or self.tintColor from a UIView subclass.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 6
    I was going to say that you can just use `UIView().tintColor`, but in fact you cannot. Not sure at what point the UIView gets the tint color set... – Dan Rosenstark Apr 08 '16 at 17:21
  • 1
    @DanRosenstark I think the `UIWindow` or maybe its root view has the original version. Views inherit their tint color from higher views in the responder chain, but in your example the new view has no superview. – Aaron Brager Apr 09 '16 at 05:20
  • 2
    `self.view.tintColor` from within `UIViewController.viewDidLoad()` gives the right blue. – Nicolas Miari Oct 06 '16 at 03:34
234

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

screenshot showing Colors window

Greg
  • 33,450
  • 15
  • 93
  • 100
91

iOS 7 default blue color is 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];
rdurand
  • 7,342
  • 3
  • 39
  • 72
tarum
  • 935
  • 5
  • 2
52

According to the documentation for 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.

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

self.view.tintColor
Josh Tynjala
  • 5,235
  • 3
  • 23
  • 25
Scott Carter
  • 1,254
  • 9
  • 16
26

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;
}
Rick
  • 847
  • 8
  • 12
  • you don't need dispatch once for main thread stuff. A simple if nil will suffice. – malhal Feb 07 '14 at 23:52
  • 6
    Why assume the method will only be called from the main thread? The overhead to `dispatch_once` is quite low and is a single if check in the common case. – Rick Feb 10 '14 at 16:24
  • @Rick Because UIKit APIs are not background thread-safe, so calling it outside the main thread is not allowed anyway. – Andrey Tarantsov Jun 24 '14 at 12:53
  • @AndreyTarantsov that is true, but it _is_ safe to use `UIColor` on multiple threads. Wrapping it in a `dispatch_once` allows for safely retrieving this color on any thread. And again, the overhead is very low. – Rick Jun 27 '14 at 16:39
  • @Rick I understand about the overhead. But you're not just touching UIColor, you're creating an UIView here, which, to the best of my knowledge, is not safe to do on non-main threads. (I may be mistaken, though; I'm not sure where it is documented to check.) – Andrey Tarantsov Jun 29 '14 at 15:19
  • 3
    @AndreyTarantsov from a quick test it seems to work fine. From the UIVIew [documentation](https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html): "Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself but all other manipulations should occur on the main thread." – Rick Jun 30 '14 at 12:19
  • @Rick A quick test does not really help here. However, the docs seem to indicate that your usage is probably fine. So I'm receding my complaint, and I'll just leave a general advise to avoid things that are asking for trouble. – Andrey Tarantsov Jun 30 '14 at 16:09
22

Hex Color code

#007AFF

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

ps. iOS, Swift

Ego Slayer
  • 1,987
  • 2
  • 22
  • 17
19

swift 4 way:

extension UIColor {
  static let system = UIView().tintColor!
}
Dmitry Kozlov
  • 1,115
  • 10
  • 14
  • I strongly not recommend you to use forced unwrapping, since it could cause application crash, if default behavior of UIKit Framework will be changed in any next version. Also getting tint color from an UIView instance couldn't guarantee that this color is a system tint color. – Stanislau Baranouski Oct 10 '18 at 08:17
  • 1
    @StanislauBaranouski Why they should change that? Please explain your point – Dmitry Kozlov Feb 24 '19 at 09:37
  • no need explanation here, it's just a bad practice to use forced unwrapping, especially if you can avoid it. – Stanislau Baranouski Feb 28 '19 at 16:43
17

Native extension with predefined system colors gives what you're looking for:

// 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
Stanislau Baranouski
  • 1,425
  • 1
  • 18
  • 22
  • 1
    This one will create UIButton every time you ask for systemBlue its better to init this color once. Using internal word in internal extension is not user friendly. Same for using class instead of static. And calling it "Blue" is not right, cause this color can be changed later like in macOS Mojave. So `static let system = UIView().tintColor!` is still much better than your variant. – Dmitry Kozlov Oct 09 '18 at 10:25
  • @DmitryKozlov you right, better to use static due memory performance. Thanks for pointing to that. But Calling it "blue" still works up to iOS 12 and it's not related to macOS at all. For macOS you have to deal with NSColor type. – Stanislau Baranouski Oct 10 '18 at 08:06
14

Get the color automatically by using this code:

static let DefaultButtonColor = UIButton(type: UIButtonType.System).titleColorForState(.Normal)!
Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
10

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

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

This gave the proper blue tint seen in a UIBarButtonItem

cscott530
  • 1,658
  • 16
  • 25
7

From iOS 7 there is an API and you can get (and set) the tint color with:

self.view.tintColor

Or if you need the CGColor:

self.view.tintColor.CGColor
6

In many cases what you need is just

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

or for swift

self.tintColor
// or if in a ViewController
self.view.tintColor
Ali
  • 18,665
  • 21
  • 103
  • 138
4

Please don't mess with view.tintColor or extensions, but simply use this:

UIColor.systemBlue
Ely
  • 8,259
  • 1
  • 54
  • 67
3

while setting the color you can set color like this

[UIColor colorWithRed:19/255.0 green:144/255.0 blue:255/255.0 alpha:1.0]
Anurag Soni
  • 1,077
  • 10
  • 14
2

Adding a category to UIColor the following way will make it available to you anytime you need it or even change its definition accross your code:

@interface UIColor (iOS7Colors)

+ (instancetype)iOS7blueColor;

@end

@implementation UIColor (SpecialColors)

+ (instancetype)iOS7blueColor;
{
    return [UIColor colorWithRed:0.0f green:0.22f blue:122.0/255.0 alpha:1.0f];
}

Once you import the Category in your code you can call the color by using:

UIColor *myBlueColor = [UIColor iOSblueColor];
  • Please, try to read this http://stackoverflow.com/help/deleted-answers, to get more understanding how to **not** answer. Namely: "Answers that do not fundamentally answer the question": **barely more than a link to an external site** – Radim Köhler Nov 20 '13 at 03:23
  • Uh how'd we get from `blue` to `red` in `+ (instancetype)iOS7redColor;` – ta.speot.is Oct 15 '15 at 06:50