简体   繁体   中英

Changing the Tint Color of UIBarButtonItem

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.

在此处输入图片说明

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.

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

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

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
}

在此处输入图片说明

斯威夫特 5

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

要更改导航栏中特定项目(例如按钮)的颜色:在 Objective-C 中

myButton.tintColor = [UIColor redColor];

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;
UITabBar.appearance().tintColor = UIColor.yellowColor()

这对我有用

AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM