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

153,056

Solution 1

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

Solution 2

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

screenshot showing Colors window

Solution 3

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

Solution 4

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

Solution 5

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;
}
Share:
153,056

Related videos on Youtube

Joel H.
Author by

Joel H.

After receiving my B.S. in Computer Science from Florida State, I moved to DC and have been working in the software development field since 2009. I've worked mostly with Ruby On Rails, Java and iOS. There is always more to learn in the software field, so I'm very thankful that StackOverflow exists. I also love playing music, writing poetry and blogging!

Updated on July 08, 2022

Comments

  • Joel H.
    Joel H. almost 2 years

    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.
    Joel H. over 10 years
    Interestingly, the default blue on my system appears to be 0:128:255 (using the same tool). I wonder if Apple changed it recently?
  • Radim Köhler
    Radim Köhler over 10 years
    Please, try to read this 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
  • Jason Moore
    Jason Moore over 10 years
    And if you prefer hex: 0x007aff
  • Richard J. Ross III
    Richard J. Ross III over 10 years
    @JoelH. Check the color space you're currently using.
  • Adil Malik
    Adil Malik over 10 years
    Why is it needed to write /255.0 with each rgb value?
  • malhal
    malhal over 10 years
    you don't need dispatch once for main thread stuff. A simple if nil will suffice.
  • malhal
    malhal over 10 years
    Because colours on macs are a float between 0.0 and 1.0.
  • Rick
    Rick about 10 years
    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.
  • Zev Eisenberg
    Zev Eisenberg almost 10 years
    Be careful in how you use this, as it may change in subsequent iOS releases.
  • Andrey Tarantsov
    Andrey Tarantsov almost 10 years
    @Rick Because UIKit APIs are not background thread-safe, so calling it outside the main thread is not allowed anyway.
  • Rick
    Rick almost 10 years
    @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.
  • Andrey Tarantsov
    Andrey Tarantsov almost 10 years
    @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.)
  • Rick
    Rick almost 10 years
    @AndreyTarantsov from a quick test it seems to work fine. From the UIVIew documentation: "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."
  • Andrey Tarantsov
    Andrey Tarantsov almost 10 years
    @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.
  • Cliff Helsel
    Cliff Helsel over 9 years
    Looks like the safest way to grab the color.
  • SimplGy
    SimplGy about 9 years
    This works for me. My sharedApplication() does not have a keyWindow with tintColor
  • malhal
    malhal about 9 years
    self.view.tintColor more likely
  • prewett
    prewett almost 9 years
    This isn't very future proof--if Apple changes the default tint color or lets users set default tint color your app won't get the correct color.
  • Nicolas Miari
    Nicolas Miari over 8 years
    Yes, definitely not a good idea to hard-code any value that could potentially change. Especially when there is an API to query the actual value.
  • daleijn
    daleijn over 8 years
    and in another system: #007AFF
  • ta.speot.is
    ta.speot.is over 8 years
    Uh how'd we get from blue to red in + (instancetype)iOS7redColor;
  • Jervisbay
    Jervisbay about 8 years
    In Swift: let defaultTintColor = UIColor(red: 0.0, green: 122/255, blue: 1.0, alpha: 1)
  • Dan Rosenstark
    Dan Rosenstark about 8 years
    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...
  • Aaron Brager
    Aaron Brager about 8 years
    @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.
  • timgcarlson
    timgcarlson about 8 years
    Feels a bit dirty, but it's better than hardcoding a link color that may change over time.
  • Nicolas Miari
    Nicolas Miari over 7 years
    self.view.tintColor from within UIViewController.viewDidLoad() gives the right blue.
  • Doug Amos
    Doug Amos over 6 years
    Swift version UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
  • Dmitry Kozlov
    Dmitry Kozlov over 5 years
    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.
  • Stanislau Baranouski
    Stanislau Baranouski over 5 years
    @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
    Stanislau Baranouski over 5 years
    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.
  • Dmitry Kozlov
    Dmitry Kozlov about 5 years
    @StanislauBaranouski Why they should change that? Please explain your point
  • Stanislau Baranouski
    Stanislau Baranouski about 5 years
    no need explanation here, it's just a bad practice to use forced unwrapping, especially if you can avoid it.