开发者

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.


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!


Anyone who's looking for Swift 4 solution..

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


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


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.


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)

    }


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜