Question

How can we change the global tint color on iOS7/iOS8 by code? I want to change multiple objects that use this property, but not change each one, that's why I want to use the global tint property.

Was it helpful?

Solution

Simply change the UIWindow 's tintColor in your application delegate, it's automatically passed as default to all its UIView descendants.

[self.window setTintColor:[UIColor greenColor]];

OTHER TIPS

[[UIView appearance] setTintColor:[UIColor greenColor]];

There are two ways to change your global tint color. As many mentioned above you could change self.window.tintColor in -application:didFinishLaunchingWithOptions:.

More elegant way, in my opinion, is to set Global Tint in File Inspector in your Storyboard while nothing is selected. This way your -application:didFinishLaunchingWithOptions: is cleaner.

Global Tint in File Inspector

You can specify a tint color for the entire app by setting the window’s tint property. To do this, you could use code similar to the following:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintColor = [UIColor purpleColor];
    return YES;
}

Updated for Swift 2.2

You can do this from anywhere like this:

// Get app delegate
let sharedApp = UIApplication.sharedApplication()

// Set tint color
sharedApp.delegate?.window??.tintColor = UIColor.green()

Or if you're trying to do this from AppDelegate,

self.window?.tintColor = UIColor.green()

Updated for Swift 5

Write in the App Delegate :

self.window?.tintColor = UIColor.green

Following things DID NOT WORKED for me:

navigationItem.backBarButtonItem?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR

navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR], for: .normal)

self.navigationController?.navigationBar.barStyle = UIBarStyle.black

navigationController?.navigationBar.barTintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR

navigationController?.navigationBar.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR]

Following WORKED :

  1. SET THE GLOBAL TINT COLOR FROM STORYBOARD.

OR

  1. SET THE TINT COLOR OF THE WINDOW

FOR WHOLE APP:

let sharedApp = UIApplication.sharedApplication()
sharedApp.delegate?.window??.tintColor = UIColor.green()

FOR SPECIFIC CONTROLLER:

set tint color of window while initialization and set back the default tint color of the app while deinitialization.

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        let window = UIApplication.shared.windows.first
        window?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        let window = UIApplication.shared.windows.first
        window?.tintColor = Theme.light.healthKit.BACK_BUTTON_TITLE_COLOR
    }

    deinit {
        let window = UIApplication.shared.windows.first
        window?.tintColor = Theme.light.App.DEFAULT_TINT_COLOR
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top