How can I change the text and icon colors for tabB

2019-01-10 05:38发布

How can I change the text and icon colors for UITabBar and UITabBarItems in iOS 7? The default gray text seems dim and hard to read for unselected tabbar items.

10条回答
虎瘦雄心在
2楼-- · 2019-01-10 06:05

@Usharao answer above worked for me;

My problem was on startup all my TabBarItems seemed to be in a selected state, all having the same "Blue" tinted colour. By selecting all the tabs one by one the coloured state would become corrected.

I used this code below in my AppDelegate class: (compatible for >= IOS9)

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UITabBar class]]] 
                                        setTintColor:[UIColor lightGrayColor]];
查看更多
疯言疯语
3楼-- · 2019-01-10 06:07

use self.tabBarController.tabBar.barStyle = UIBarStyleBlack; to make the tab bar black

看我几分像从前
4楼-- · 2019-01-10 06:08

Tested in iOS 8 for permanent text color (selected/unselected) and image colors (selected/unselected) without create two images with different colors foreach tab:

Text color:

[[UITabBar appearance] setTintColor: selectedTabColor ];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       ** selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       **selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];

Images color: (assuming that the original images have the color you want display as unselected)

In a UITabBarController subclass -awakeFromNib :

    for (int i =0; i<self.viewControllers.count; i++)
    {
        UITabBarItem *tab = [self.tabBar.items objectAtIndex:i];
        tab.image = [tab.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    }

Credits: the whole internet and stack overflow XD

查看更多
Root(大扎)
5楼-- · 2019-01-10 06:10
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] 
Root(大扎)
6楼-- · 2019-01-10 06:12

This worked for me, to tint not active items in the tabbar

UITabBarItem *item = [self.tabBar.items objectAtIndex:1];

// here you need to use the icon with the color you want, as it will be rendered as it is

item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// this icon is used for selected tab and it will get tinted as defined in

self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];
查看更多
Deceive 欺骗
7楼-- · 2019-01-10 06:18

There are two things you need to do for this:

1) If you want to customize the TabBar itself, you need to set the barTintColor for the tabBarController:

    // this will generate a black tab bar
    tabBarController.tabBar.barTintColor = [UIColor blackColor];

    // this will give selected icons and text your apps tint color
    tabBarController.tabBar.tintColor = appTintColor;  // appTintColor is a UIColor *

2) Set the tabBarItem text appearance for each state that you want to override:

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : appTintColor
                                                    } forState:UIControlStateSelected];


// doing this results in an easier to read unselected state then the default iOS 7 one
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];
查看更多
登录 后发表回答