When I set the background color of a UITabBar iOS automatically lightens this color since the default UITabBar is translucent.
But I would like to use a UITabBar which is NOT translucent. In iOS 12 and below I solved this by setting a background image of the desired color:
// Create an image from a given color using a custom extension
[[UITabBar appearance] setBackgroundImage:[UIImage colorImageWithColor:[UIColor redColor]]];
This works fine. However, I would like to use the new Dark Mode in iOS 13. Obviously this cannot be done when using a colored background image instead of a background color. Instead not without reacting manually to the appearance change to switch to another color image.
Using named colors would be way better IF it would be possible to tell iOS not to draw the `UITabBar translucent.
If I try to disable the translucent effect the UITabBar become all white instead of the specified color.
[[UITabBar appearance] setTranslucent:false];
How to solve this?
I managed to solve the problem using a custom UITabBar subclass.
When the app launches the dynamic color MyDynamicTabBarBG is set as non-translucent background by creating an image from this color
Changes between Normal and Dark Mode while the app is active are detected using traitCollectionDidChange. Than the dynamic color is simply re-applied by creating a new image.
Code:
#implementation MCTabBar
- (void)awakeFromNib {
[super awakeFromNib];
// Create a color image from a given color using a custom extension
[self setBackgroundImage:[UIImage colorImageWithColor:[UIColor colorNamed:#"MyDynamicTabBarBG"]]];
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[self setBackgroundImage:[UIImage colorImageWithColor:[UIColor colorNamed:#"MyDynamicTabBarBG"]]];
}
#end
This works but is quite hacky. Is there really no more elegant solution?
I tried doing it like described in How to change Navigation Bar color in iOS 7? but unfortunatley it does not seem to work for me.
The color only changes to a light version. Since a screenshot describes it better than a thousand words here you go:
use this code work for me
[[UINavigationBar appearance]setBarTintColor:[UIColor colorWithRed:95/255.f green:154/255.f blue:201/255.f alpha:1]];
using storyboard
i want to modify my code to change colour of UINavigationBar and UISearchBar.
For some odd reason, the same instance of UIColor provide different results. Please take a look at screen:
There is the code for customise appearance:
UIColor *color = [UIColor colorWithR:88 G:123 B:139 A:1];
[[UINavigationBar appearance]setBarTintColor:color];
[[UISearchBar appearance] setBarTintColor:color];
ColorWithR is a simple category that simplify process of getting colour using RGB. As you can see, i have same colour but if you look at screen, it different. How could this happen?
PS. Im sorry for this extremely large screenshot, i was unable to figure out how to make it small.
I set a special color for my Nav Bar in the AppDelegate:
[[UINavigationBar appearance]setBarTintColor:[UIColor colorWithRed:0 green:(41.0f/255.0f) blue:(103.0f/255.0f) alpha:1]];
But the RGB Value which is then displayed is: R:12 G:48 B:110.
It seems that iOS just puts the values a bit higher. How can I avoid that? (Because it looks ugly)
And when I give the same color an customized TableViewSection it is right.
Screenshots:
NavBar:
Customized TableViewSection:
Ok they were translucent. Unchecked translucent in StoryBoard, works fine now;)
Try adding this to your AppDelegate:
[[UINavigationBar appearance] setTranslucent:NO];
I had a similar problem...i tried to replace expression in brackets (41.0f/255.0f) by value after divison (~0.1608)...and it works to me)
I need to change the selection color of UITabBar from default blue to red. How do we do this.
Update September 2017:
It's been two years since I've written this answer and since it's receiving upvotes regularly, I should say this is probably the worst possible answer to this question, it's error prone, likely to break because of iOS updates, hard to debug, etc., so please don't do the things I've written and apply better solutions such as subclassing UITabBar or UITabBarController. Thanks.
You can do this by setting a "tintColor" attribute (Key Path) for you UITabBar.
Select the UITabBar in the document outline. (NOT the Controller with the yellow icon.)
Select Identity Inspector in the Utilities area.
Click the + in "User Defined Runtime Attributes."
Add a "tintColor" Key Path of type "Color" and the color you want.
This should do it. You can check it against the screenshot below.
More on this:
There's a "Tint" attribute in Identity Inspector of UITabBar which I believed would do the exact same thing but apparently, it does nothing. It's default value is the exact default fill color when a UITabBarItem is selected, so my guess is it would be fixed in the stable release Xcode 7. Fingers crossed.
In IOS5, UITabBar has a selectedImageTintColor property which does what you need.
In iOS 7 it's simply the tintColor. One way to accomplish this could be to subclass UITabBarViewController, set the custom class in the storyboard, and in your viewDidLoad method of the subclassed tabBarVC add this:
[[self tabBar] setTintColor:[UIColor redColor]];
To achieve above result perform following steps.
Step 1: Add your desired images in Assets.xcassets, and make sure they Render As: Default
Step 2: Select your UITabBar object and set Image Tint color, this color will be selected tab color
Step 3: Select UITabBar object and add Key Path: unselectedItemTintColor, Type: Color, Value: Choose color for unselected item in User Defined Runtime Attributes.
All done.
It is extremely easy
Create a custom class of UITabBarController and in -(void)viewDidLoad method add this line:
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
Because UITextAttributeTextColor is deprecated in iOS 7, you should use:
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
Starting from iOS 8 it's as simple as:
UITabBar.appearance().tintColor = UIColor.redColor()
Simply change the following property in Interface Builder for the TabBar
Obviously in my case its White.
The SDK does not make this easy, but it is technically possible. Apple apparently believes this to be part of their vision of a consistent look and feel.
UITabBar is a subclass of UIView. You can always subclass and implement your own -drawRect:
This is not a trivial task, however, you have to essentially re-implement the class from scratch or you risk some weird side-effects.
Swift 5 Programatically
It is pretty easy in Swift 5.
In your TabBarController write this:
tintColor = UIColor.red
That's it
I've been searching for a way to set the selected text color of a UITabBarItem and have found a dead simple method, using the UIAppearance protocol.
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
Please excuse the awful colors!
iOS 5.0 fixes this issue but the solution is under NDA. Look up UITabBar in your documentation for an EASY way to do what you want to do.
I found the easiest solution -
Select Tab Bar in Tab Bar Controller
Set Image Tint color
Set Tint Color
For reference see the attached image.