How can I get the iOS 7 default blue color programmatically?
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]
.
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;
}
Related videos on Youtube
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, 2022Comments
-
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:
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. over 10 yearsInterestingly, 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 over 10 yearsPlease, 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 over 10 yearsAnd if you prefer hex:
0x007aff
-
Richard J. Ross III over 10 years@JoelH. Check the color space you're currently using.
-
Adil Malik over 10 yearsWhy is it needed to write
/255.0
with eachrgb
value? -
malhal over 10 yearsyou don't need dispatch once for main thread stuff. A simple if nil will suffice.
-
malhal over 10 yearsBecause colours on macs are a float between 0.0 and 1.0.
-
Rick about 10 yearsWhy 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 almost 10 yearsBe careful in how you use this, as it may change in subsequent iOS releases.
-
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 almost 10 years@AndreyTarantsov that is true, but it is safe to use
UIColor
on multiple threads. Wrapping it in adispatch_once
allows for safely retrieving this color on any thread. And again, the overhead is very low. -
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 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 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 over 9 yearsLooks like the safest way to grab the color.
-
SimplGy about 9 yearsThis works for me. My
sharedApplication()
does not have akeyWindow
withtintColor
-
malhal about 9 yearsself.view.tintColor more likely
-
prewett almost 9 yearsThis 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 over 8 yearsYes, 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 over 8 yearsand in another system: #007AFF
-
ta.speot.is over 8 yearsUh how'd we get from
blue
tored
in+ (instancetype)iOS7redColor;
-
Jervisbay about 8 yearsIn Swift: let defaultTintColor = UIColor(red: 0.0, green: 122/255, blue: 1.0, alpha: 1)
-
Dan Rosenstark about 8 yearsI 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 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 about 8 yearsFeels a bit dirty, but it's better than hardcoding a link color that may change over time.
-
Nicolas Miari over 7 years
self.view.tintColor
from withinUIViewController.viewDidLoad()
gives the right blue. -
Doug Amos over 6 yearsSwift version
UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
-
Dmitry Kozlov over 5 yearsThis 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 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 over 5 yearsI 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 about 5 years@StanislauBaranouski Why they should change that? Please explain your point
-
Stanislau Baranouski about 5 yearsno need explanation here, it's just a bad practice to use forced unwrapping, especially if you can avoid it.