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
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 :
OR
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
}
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]];
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.
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()
[[UIView appearance] setTintColor:[UIColor greenColor]];