Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
980 views
in Technique[技术] by (71.8m points)

ios - 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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...