ChatGPT解决这个技术问题 Extra ChatGPT

Change tab bar tint color on iOS 7

Is there a way to change the tint of a tab bar on iOS 7 from the default white with blue icons to another color tint with different color buttons?

You may have to subclass it. I have never tried so I am not 100% sure, but this seems a possible solution

T
Tarek Hallak

Try the below:

[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];

To tint the non active buttons, put the below code in your VC's viewDidLoad:

UITabBarItem *tabBarItem = [yourTabBarController.tabBar.items objectAtIndex:0];

UIImage *unselectedImage = [UIImage imageNamed:@"icon-unselected"];
UIImage *selectedImage = [UIImage imageNamed:@"icon-selected"];

[tabBarItem setImage: [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage: selectedImage];

You need to do this for all the tabBarItems, and yes I know it is ugly and hope there will be cleaner way to do this.

Swift:

UITabBar.appearance().tintColor = UIColor.red

tabBarItem.image = UIImage(named: "unselected")?.withRenderingMode(.alwaysOriginal)
tabBarItem.selectedImage = UIImage(named: "selected")?.withRenderingMode(.alwaysOriginal)

Perfect. Thanks. One more quick question, is there a way to tint the non active buttons in the tab bar, (they are gray when not selected)?
setFinishedSelectedImage is deprecated in iOS7.
@null Code for non active buttons has forState:UIControlStateNormal which changes color of all tab bar items (selected and non-selected) in my application. There's other state UIControlStateSelected but didn't see non-selected.
[[UITabBarItem appearance] setTitleTextAttributes.. did not change the color of the unselected items, they remain gray.
Use UIControlStateNormal for all tabbar items, use UIControlStateSelected for selected items, example: [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
h
herdi

There is an much easier way to do this.

Just open the file inspector and select a "global tint".

You can also set an app’s tint color in Interface Builder. The Global Tint menu in the Interface Builder Document section of the File inspector lets you open the Colors window or choose a specific color.

Also see:

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html


Global tint menu not available for non-storyboard nibs.
This would be great except it doesn't work for me in Xcode 5.
Doesn't work, Xcode 5.1. "window.tintColor = [UIColor purpleColor]" does.
The Global Tint setting of the Interface Builder Document doesn't actually allow you to set the tint for an app. It allows you to set the tint for a Storyboard - if you have multiple, you can mix and match or set it in code as above.
How odd. I had just tried setting the tint color in the Storyboard, in iOS 7, Xcode 5.1.1 and it does nothing for a Storyboard created UITabBar.
M
MaxEcho

iOS 7.1.1

If someone is going to need to use globally setting tint color:

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

In didFinishLaunchingWithOptions of AppDelegate.

Also below code will change only tab bar tint color in any viewDidLoad method:

[self.tabBarController.tabBar setTintColor:[UIColor redColor]];

If you use this option, can you still override the color on a screen-by-screen basis?
With the code on second line you can override the color for each screen seperately
This is the only solution I have found that works from a VC's VDL in iOS 7 for the current tab item. [self.tabBarController.tabBar setTintColor:[UIColor orangeColor]];
S
Sofi Software LLC

In app delegate didFinishLaunchingWithOptions:

window.tintColor = [UIColor purpleColor];

sets the tint color globally for the app.


Does nothing on iOS 7.1 in Xcode 5.1.1. Even when using _window in the AppDelegate.
J
Jake Chasan

Write this in your View Controller class of your Tab Bar:

// Generate a black tab bar
self.tabBarController.tabBar.barTintColor = [UIColor blackColor];

// Set the selected icons and text tint color
self.tabBarController.tabBar.tintColor = [UIColor orangeColor];

P
Peter Mortensen

What finally worked for me was:

[self.tabBar setTintColor:[UIColor redColor]];
[self.tabBar setBarTintColor:[UIColor yellowColor]];

佚名

In "Attributes Inspector" of your Tab Bar Controller within Interface Builder make sure your Bottom Bar is set to Opaque Tab Bar:

https://i.stack.imgur.com/IzKPv.png

Now goto your AppDelegate.m file. Find:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

And then add this code between the curly braces to change the colors of both the tab bar buttons and the tab bar background:

///----------------SET TAB BAR COLOR------------------------//

//--------------FOR TAB BAR BUTTON COLOR---------------//
[[UITabBar appearance] setTintColor:[UIColor greenColor]];

//-------------FOR TAB BAR BACKGROUND COLOR------------//
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];

d
dfinki

After trying out all the suggested solutions, I couldn't find any very helpful.

I finally tried the following:

[self.tabBar setTintColor:[UIColor orangeColor]];

which worked out perfectly.

I only provided one image for every TabBarItem. Didn't even need a selectedImage.

I even used it inside the Child-ViewControllers to set different TintColors:

UIColor *theColorYouWish = ...;
if ([[self.parentViewController class] isSubclassOfClass:[UITabBarController class]]){
    UITabBarController *tbc = (UITabBarController *) self.parentViewController;
    [tbc.tabBar setTintColor:theColorYouWish];
}

P
Peter Mortensen

You can set your tint color and font as setTitleTextattribute:

UIFont *font= (kUIScreenHeight>KipadHeight)?[UIFont boldSystemFontOfSize:32.0f]:[UIFont boldSystemFontOfSize:16.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,
                            tintColorLight, NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];