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

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?

This question is related to ios colors ios7 uicolor

The answer is


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

UIColor.systemBlue

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

screenshot showing Colors window


swift 4 way:

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

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

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

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

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

Get the color automatically by using this code:

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

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


Hex Color code

#007AFF

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

ps. iOS, Swift


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]

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

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

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

Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to colors

is it possible to add colors to python output? How do I use hexadecimal color strings in Flutter? How do I change the font color in an html table? How do I print colored output with Python 3? Change bar plot colour in geom_bar with ggplot2 in r How can I color a UIImage in Swift? How to change text color and console color in code::blocks? Android lollipop change navigation bar color How to change status bar color to match app in Lollipop? [Android] How to change color of the back arrow in the new material theme?

Examples related to ios7

Xcode: Could not locate device support files How to Apply Gradient to background view of iOS Swift App How do I hide the status bar in a Swift iOS app? Using Predicate in Swift How do I programmatically set device orientation in iOS 7? What is the height of Navigation Bar in iOS 7? Warning :-Presenting view controllers on detached view controllers is discouraged Color Tint UIButton Image iOS change navigation bar title font and color How to embed small icon in UILabel

Examples related to uicolor

How to use hex color values Programmatically create a UIView with color gradient How can I change image tintColor in iOS and WatchKit How to change the status bar background color and text color on iOS 7? How can I get the iOS 7 default blue color programmatically? Making RGB color in Xcode Can not change UILabel text color How can I create a UIColor from a hex string?