kiran kumar
kiran kumar

Reputation: 1359

Setting text-color on selected tab bar item

I've created a tab bar application. I have four tabs; on the selected tab, I need to set the color red for tab title. How can i do that?

Thanks in advance.

Upvotes: 9

Views: 8550

Answers (7)

Gaurav Chandarana
Gaurav Chandarana

Reputation: 754

Anyone who's looking for Swift 4 solution..

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: .red], for: .selected)

Upvotes: 5

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7444

This is the swift version :-

    for item in self.mainTabBar.items! {

    let unselectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    let selectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    item.setTitleTextAttributes(unselectedItem as? [String : AnyObject], forState: .Normal)
    item.setTitleTextAttributes(selectedItem as? [String : AnyObject], forState: .Selected)

    }

Upvotes: 1

Ben Lachman
Ben Lachman

Reputation: 3100

Just to clear things up a bit…

If you'd like to change the appearance for all tab bar items, use:

Objective-C:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];

Swift:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)

However, if you just want to set the appearance of a single item do it like so:

Objective-C:

[self.tabBarItem setTitleTextAttributes:@{UITextAttributeTextColor :[UIColor someColor]} forState:UIControlStateSelected];

Swift:

tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.someColor()], forState: .Selected)

Note: tabBarItem is a property on UIViewController. This means that while every UIViewController has this property, it may not be the tabBarItem you're looking for. This is often the case when your view controller is enclosed in a UINavigationController. In this instance, access the tabBarItem on the navigation controller not the one in it's root (or other) view controller.

Upvotes: 2

Rich Fox
Rich Fox

Reputation: 2194

This is what finally worked for me:

1)Selected text color

[[UIView appearance] setTintColor:someColor];

2)Unselected text(also changes image color)

[[UITabBar appearance] setTintColor:anotherColor];

Upvotes: 2

Ben Clayton
Ben Clayton

Reputation: 82257

Using the UIAppearance protocol (iOS5+) this is now possible, and actually pretty easy.

[UITabBarItem.appearance setTitleTextAttributes:@{
        UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];

[UITabBarItem.appearance setTitleTextAttributes:@{
        UITextAttributeTextColor : [UIColor purpleColor] }     forState:UIControlStateSelected];

Please excuse the awful colors!

Upvotes: 24

Iphone_bharat
Iphone_bharat

Reputation: 1159

That is possible by -drawRect:, but by doing that you are highly increasing chances of your app being rejected by App Store

Upvotes: -1

Jonathan Sterling
Jonathan Sterling

Reputation: 18385

If I understand your question correctly, you want to customize the color of the text on the UITabBarItems. Unfortunately, it's really not that flexible. If you're intent on doing this (which, unless you've carefully considered the design with the help of a professional, I recommend against!), you'll have to do some really frightening things to get this to work.

I'd suggest iterating through the subviews of the UITabBar (as many levels as necessary), and looking for UILabel objects. If you find some, you can change their color. If you don't, that means that it is implemented differently (probably in a -drawRect: method somewhere); if this happens to be the case, you really ought to give up.

Best of luck on whatever you decide to do.

Upvotes: -1

Related Questions