Setting text-color on selected tab bar item

。_饼干妹妹 提交于 2019-11-28 23:45:42

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.

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!

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 what finally worked for me:

1)Selected text color

[[UIView appearance] setTintColor:someColor];

2)Unselected text(also changes image color)

[[UITabBar appearance] setTintColor:anotherColor];

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)

    }

Anyone who's looking for Swift 4 solution..

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!