50

I have a project using Storyboards and whenever I push a view controller with a segue, the dynamically created bar button item is always blue.

enter image description here

It's driving me nuts. Because this object is created dynamically, I cannot set its color in IB (like I have done with previous bar button items).

Among the solutions I have tried are:

  1. Set it in the receiver's viewDidLoad
  2. Set it in the receiver's viewDidAppear

    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];

  3. When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

  1. I have tried the following code (which I got from other SO answers) in my app's delegate, when the new view gets called, and before pushing the new view:

    [[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];

All the google answers I have found recommend to use the code above, but it's not working at all for me. Maybe there are some changes in iOS 7's appearance API? No matter how or where I try to set "Categorías" to white, it's always the default blue.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60
Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100

8 Answers8

87

In iOS 7, to set the color of all barButtonItems in your app, set the tintColor property on the application's window in the AppDelegate.

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

More detailed info in Apple's iOS 7 UI Transition Guide (Specifically under the 'Using Tint Color` section).

***OR***

Based on some of the comments, you can also achieve this with the UINavigationBar appearance proxy. This will affect the tintColor of only UIBarButtonItems, as opposed to setting the tintColor on the window and affecting all subviews of that window.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
        [UINavigationBar appearance].tintColor = [UIColor whiteColor];
    }

    return YES;
}
hgwhittle
  • 9,316
  • 6
  • 48
  • 60
  • Any color other than white does not work as expected – RK- Feb 07 '14 at 10:34
  • Define "does not work as expected". Any color that I set `self.window.tintColor` to becomes the color of my barButtonItems. As expected. – hgwhittle Feb 07 '14 at 14:05
  • I change the rootViewController of window based on logout and login. When I set self.window.tintColor any color other than whiteColor, it works, only for the first time. Then when I change the window.rootViewController to something else, the color dims (not disabled, ) and this case does not occur when whiteColor is set as the tintColor – RK- Feb 07 '14 at 15:42
  • 2
    I can only assume that something quirky is happening somewhere in your rootViewController logic which is affecting the tintColor. I would look at Apple's documentation on how the tintColor inheritance works under the cover. – hgwhittle Feb 07 '14 at 15:50
  • I fixed this problem...A popover was present still when I changed the rootViewController of the window...Now before setting window.rootViewController to a new view controller , I dismiss the popovercontroller – RK- Feb 11 '14 at 13:38
  • This also makes text cursor white, so not always an appropriate solution. – Alex B Mar 22 '14 at 17:10
  • @AlexB - Yes if you set the window's tintColor to white, any `UITextField` tintColor will also be set to white (the cursor). However simply overriding the tintColor on the `UITextField` will resolve the issue. `theTextField.tintColor = [UIColor blueColor]; //sets cursor to blue` – hgwhittle Mar 24 '14 at 13:06
  • Rather than setting the tint on the entire window, you can also just set it on the nav bar items using `[UINavigationBar appearance].tintColor = [UIColor colorWithWhite:0.7 alpha:1.0];` – Sandy Chapman Jun 04 '14 at 19:42
  • But how to have two different colors for two UIBarButtons in navigation bar at the same time? Struggling for the whole day without no luck... – Centurion Jul 02 '14 at 18:51
25

I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

See "Appearance of Navigation Bars" section: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

Yuriy Panfyorov
  • 523
  • 5
  • 5
  • 14
    `self.navigationController.navigationBar.tintColor = [UIColor whiteColor];` does not set the color of the `UIBarButtonItem`s – codeperson May 07 '14 at 01:26
  • 2
    No, it does set it - look at the link above, "Appearance of Navigation Bars" section. Actually, it's the only right way of implementation. To change tint of the bar itself you should use barTintColor. – Igor Vasilev Aug 06 '14 at 16:47
  • Argh... somehow I encountered the situation where `UINavigationController.navigationBar.tintColor = ...` does not take any effect... My work around in this case was `UIViewController *currentViewController = self.childViewControllers.lastObject; [currentViewController.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull barButtonItem, NSUInteger idx, BOOL * _Nonnull stop) { barButtonItem.tintColor = YOUR_COLOR; }]; // REPEAT SAME THINGS FOR RIGHT BAR BUTTON ITEMS AND BACK BUTTON ITEM` Definitely not ideal, but it may help – barley Feb 14 '16 at 18:15
10

In Swift 3.0

let navigationBarAppearnce = UINavigationBar.appearance()

A navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images.

navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)

The barTintColor property affects the color of the bar itself

navigationBarAppearnce.tintColor = UIColor.white

Final Code

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

 let navigationBarAppearnce = UINavigationBar.appearance()

 navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)

 navigationBarAppearnce.tintColor = UIColor.white

 navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

 //Change status bar color
 UIApplication.shared.statusBarStyle = .lightContent

 return true
}

enter image description here

Ashok R
  • 19,892
  • 8
  • 68
  • 68
7

Swift 5

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
Mithra Singam
  • 1,905
  • 20
  • 26
3

To change the color of a specific item (e.g. a button) in your nav bar: In Objective-C

myButton.tintColor = [UIColor redColor];
Vette
  • 511
  • 5
  • 10
2

In iOS 8 if you changed UIView tint color for some purpose, for example for branding UIAlertView, tint color for UIBarButtonItem in UIToolBar also changed that way. To fix this, just write this code

[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;

For UIBarButtonItem tint color in UINavigationBar use standard method

[UINavigationBar appearance].tintColor = BLACK_COLOR;
FunkyKat
  • 3,233
  • 1
  • 23
  • 20
0
UITabBar.appearance().tintColor = UIColor.yellowColor()
benka
  • 4,732
  • 35
  • 47
  • 58
user2643679
  • 706
  • 12
  • 18
  • 2
    wrong bar hombre. This ones asking about the navbar. The solution is ALMOST correct however. – Shayne Jun 04 '19 at 03:24
0

This worked for me

AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
yehyatt
  • 2,295
  • 3
  • 28
  • 31