How can I change the text and icon colors for tabBarItems in iOS 7?

前端 未结 11 903
陌清茗
陌清茗 2020-12-02 07:26

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.

相关标签:
11条回答
  • 2020-12-02 08:21

    This Should Work Perfectly for iOS 8 also

    For unselected tabbar item:

    [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor: [UIColor whiteColor]];
    

    For selected tabbar item:

    [[UITabBar appearance] setTintColor:[UIColor orangeColor]];
    
    0 讨论(0)
  • 2020-12-02 08:23

    @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]];
    
    0 讨论(0)
  • 2020-12-02 08:24

    Ed's answer is perfect, but let me add one thing. TabBar is in default translucent thus affected by the color of view under the TabBar (i.e. each member viewController's view's color affects TabBar appearance.).

    So I set below code not to be affected.

    self.tabBarController.tabBar.translucent = false;
    

    Together with Ed's answer here is a complete code I use now.

    self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    self.tabBarController.tabBar.translucent = false;
    self.tabBarController.tabBar.tintColor = [UIColor blueColor];
    
    0 讨论(0)
  • 2020-12-02 08:28

    Now from iOS10 one can use

    @property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor
    

    to change default color for TabBarItem image and text at unselected state.

    Thus, the pair of properties tintColor and unselectedItemTintColor gives us full control on items colors.

    0 讨论(0)
  • 2020-12-02 08:32

    You try it

    for (UITabBarItem *item in self.tabBarController.tabBar.items) {
            item.image = [[UIImage imageNamed:@"youimage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
            [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                               [UIColor whiteColor], UITextAttributeTextColor,
                                                               nil] forState:UIControlStateNormal];
    
            item.selectedImage = [UIImage imageNamed:@"youimage.png"];
        }
    
    0 讨论(0)
提交回复
热议问题