How can I change a color of image and label on UIT

2019-03-06 08:30发布

How can I change a color of image and label on UITabBar on iOS 7.1? On iOS 7 I could make it by Tint property. But on iOS 7.1 it doesn't work.

3条回答
别忘想泡老子
2楼-- · 2019-03-06 09:05

It works in the same way in iOS 7 and iOS 7.1!

In AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Add this if you only want to change Selected Image color 
    // and/or selected image text
    [[UITabBar appearance] setTintColor:[UIColor redColor]];

    // Add this code to change StateNormal text Color,
    [UITabBarItem.appearance setTitleTextAttributes:
    @{NSForegroundColorAttributeName : [UIColor greenColor]} 
    forState:UIControlStateNormal];

    // then if StateSelected should be different, you should add this code
    [UITabBarItem.appearance setTitleTextAttributes:
    @{NSForegroundColorAttributeName : [UIColor purpleColor]} 
    forState:UIControlStateSelected];

    return YES;
}

In every ViewController: (if you want to change the unselected image color)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // changing the unselected image color, you should change the selected image 
    // color if you want them to be different
    self.tabBarItem.selectedImage = [[UIImage imageNamed:@"yourImage_selectedImage"]
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.tabBarItem.image = [[UIImage imageNamed:@"yourImage_image"] 
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

The clue of this code is 'UIImageRenderingModeAlwaysOriginal':

Rendering Modes by Apple Documentation:

UIImageRenderingModeAutomatic,          // Use the default rendering mode for the context where the image is used    
UIImageRenderingModeAlwaysOriginal,     // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate,     // Always draw the image as a template image, ignoring its color information
查看更多
手持菜刀,她持情操
3楼-- · 2019-03-06 09:21

This changes the tint of both the image and the label, when selected.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
}
查看更多
一夜七次
4楼-- · 2019-03-06 09:27

UITabBarItem has never had a tint property. No built-in class has ever had a tint property. Nothing has changed here.

In iOS 7 and 7.1, a UITabBar has a tintColor property because it is a UIView.

查看更多
登录 后发表回答