How can I change a color of image and label on UITabBar on iOS 7.1?

后端 未结 3 1732
遥遥无期
遥遥无期 2021-01-27 15:16

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条回答
  • 2021-01-27 15:49

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

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [[UITabBar appearance] setTintColor:[UIColor redColor]];
    }
    
    0 讨论(0)
  • 2021-01-27 15:55

    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
    
    0 讨论(0)
  • 2021-01-27 16:04

    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.

    0 讨论(0)
提交回复
热议问题