17

I'm developing a little app and I would like to change the default global tint color from blue to orange. I've seen many ways to do it in Objective-C but I can't seem to get it to work using swift. How could I achieve this? Thanks in advance!

José María
  • 2,835
  • 5
  • 27
  • 42

2 Answers2

44

This answer was last revised for Swift 5.2 and iOS 13.5 SDK.


You can set the tintColor on your window. Because tint color cascades down to all subviews (unless explicitly overridden), setting it on a window will effectively make it global inside that window.

Add the following line to your application(_:didFinishLaunchingWithOptions:) or scene(_:willConnectTo:options:) just before making the window key and visible:

window.tintColor = .systemOrange /* or .orange on iOS < 13 */

You can also set in in the storyboard by changing the Global Tint property:

akashivskyy
  • 44,342
  • 16
  • 106
  • 116
  • When I add the code you provided me, the view doesn't load and this shows up in the console: "Application windows are expected to have a root view controller at the end of application launch" – José María Aug 02 '14 at 12:45
  • Are you using storyboards? – akashivskyy Aug 02 '14 at 12:48
  • Yes! It's sooooo easy I don't know how I couldn't come up with this earlier. Thanks for the answer! :D EDIT: I've set it there but the color isn't changing o.o – José María Aug 02 '14 at 13:33
  • That's weird. Are you sure you aren't overriding this color? Maybe implicitly in some other IB files? – akashivskyy Aug 02 '14 at 13:48
  • In case it is needed, you can check the code here: https://github.com/jmml97/Pocket-Money/tree/master – José María Aug 02 '14 at 14:10
  • 1
    It works! Try adding a stepper or a slider... If you wanted to change the tab bar items' highlight color, you have to do that explicitly, they don't respond to the `tintColor`. Also, labels have their own `textColor` property too. – akashivskyy Aug 02 '14 at 14:23
  • Oh, yeah that's what I wanted. I thought that the tint color also changed the highlight color (which would be logical). So, do you know how to do it? Thanks for your help, I really appreciate it! – José María Aug 02 '14 at 14:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58546/discussion-between-jose-maria-and-akashivskyy). – José María Aug 02 '14 at 14:55
  • Hi, I know this is old, but maybe someone has the answer to how to change the tab bar items' highlight color in swift 3 now? Thanks! – Efren May 10 '17 at 06:31
12

A small update from Apple here. Tested with Xcode Version 12.4.

There is now an AccentColor in the Color Asset catalogue which can be set in the build settings. In newer projects it is set by default but older projects might need to add it manually.

  1. Create a Color Asset with the name "AccentColor" (or whatever you like) enter image description here
  2. Go to your Build Settings of your Target and add the name of your color asset as the value for the entry "Global Accent Color Name". enter image description here

Note that AccentColor asset-name is NOT a preserved keyword, and can be anything custom, but the Build setting is what makes this work.

Basically, iOS has nothing equal to the Android theme yet (2021), where standard colors have keywords, and we can not simply set those in assets.

palme
  • 2,499
  • 2
  • 21
  • 38
  • 2
    Actual best solution. Didn't know that was a thing, pretty amazing. – JoniVR Dec 03 '21 at 08:54
  • 1
    It's also cool that you can access it with `UIColor(named:"AccentColor")`, too, which helps when establishing a uniform theme in the app. – Kyle Humfeld Mar 06 '23 at 21:49
  • Is there any way to dynamically change this, where you are able to change the colour inapp, without having to rebuild through Xcode? I currently save the user selected colour to userDefaults and set the window?.tintColor inside didFinishLaunchingWithOptions, but obviously this will only change when the app is relaunched. – Bob The Coder Mar 10 '23 at 17:42
  • I doubt it since the assets are all static. I also did not stumble upon any other way than the one you described. – palme Mar 11 '23 at 21:16