How to change UITabBar Selection color - ios

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.

Related

Defining colours programmatically and using them in Interface Builder

I want to customize the colours of an App reusing the same elements but changing the colours.
These elements are simple Views, UIButton, UILabel, etc. nothing fancy. The colours may come from a XML or plist I will preload and parse to UIColor.
Yes, I can create an outlet from each of them and set them manually, but I got hundreds of elements and I want to avoid this path.
I tried IBInspectable, without luck, I’m looking for a widespread solution, all views, all VCs.
I am coding in Objective-C by the way...
Could you suggest any approach on how should I do this?
Comment if you want more detailing…
Thank you all very much!
Couple options...
Subclass your UI elements, and use MYUIButton instead of UIButton, for example, or
Look at UIAppearance proxy - https://developer.apple.com/reference/uikit/uiappearance - Using that, you can set default appearance characteristics for the whole app.
Example:
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]];
[[UILabel appearance] setBackgroundColor:[UIColor orangeColor]];
If you include those two lines (often done in AppDelegate / didFinishLaunchingWithOptions), every UIButton in your app will have a yellow background, and every UILabel will have an orange background.
(However, you will not see these changes in Interface Builder)
Edit:
As Charles mentions in his comment, you can create subclasses and then apply appearance changes to only those classes.
Suppose you have 3 button "types" that you want to apply a "color scheme" to - dark-blue, medium-blue, light-blue or dark-red, medium-red, light-red, etc. You could take the approach of creating DarkUIButton, MediumUIButton and LightUIButton subclasses. Then, when you load your color scheme...
[[DarkUIButton appearance] setBackgroundColor:scheme.darkcolor];
[[MediumUIButton appearance] setBackgroundColor:scheme.mediumcolor];
[[LightUIButton appearance] setBackgroundColor:scheme.lightcolor];

Changing the Navigation Bar Tint changes the color not correctly

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

How to set the text color of the UISearchBar's placeholder

I have a UISearchBar with a dark background color so I was trying to change the place holder text color of UISearchBar (which will be gray by default) but I didn't find a way to set it. So I thought of getting some help :) please suggest me how this can be achieved thanks in advance :)
Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.
Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.
Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.
First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed
Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this
If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.
Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :
self.placeholder = #"My Placeholder text";
Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)
Here is the solution
[searchBar setValue:[UIColor blueColor] forKeyPath:#"_searchField.textColor"];

iOS 7 NavigationBar not showing exact RGB color

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)

Prevent UiNavigationBar Title from getting Cut off?

I am trying to customize the appearance of the navigationBar title in my ios application. This is the code I currently have:
NSMutableDictionary *navigationTitleAttributes = [NSMutableDictionary dictionary];
[navigationTitleAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[navigationTitleAttributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[navigationTitleAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[navigationTitleAttributes setValue:[UIFont fontWithName:#"Calibri" size:30] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:navigationTitleAttributes];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-8 forBarMetrics:UIBarMetricsDefault];
The code yields the following effect:
It works great but my title gets cut off from the bottom.
I've seen solutions to this problem that use a custom UIView (such as this one: UINavigationbar title is cut off when using titleTextAttributes). However, that particular solution requires that the titleView property of the navigation bar be updated for each screen.
I was wondering if there was a simple solution that would cascade through my entire application.
Thanks
Th simple solution is to not use such a large font size. If you set the size to zero then the text should be auto-sized as appropriate.
Otherwise, using a custom view is the correct solution. You can subclass the navigation controller or navigation bar in order to ensure that all of the views have the label styled in the same way.
If you're using a custom font, you may be having the same problem I was. I found a few answers on this post to be quite helpful. I changed my descender values in my .otf font file to prevent my font from being cut off on the bottom. It was especially prevalent in iOS 7.
Custom installed font not displayed correctly in UILabel

Resources