如何在iOS 7.1上更改UITabBar上的图像和标签颜色?

时间:2014-03-28 20:12:08

标签: colors uitabbar uitabbaritem ios7.1

如何更改iOS 7.1上UITabBar上的图像和标签颜色?在iOS 7上,我可以通过Tint属性来创建它。但是在iOS 7.1上它不起作用。

3 个答案:

答案 0 :(得分:1)

它在iOS 7和iOS 7.1中以相同的方式工作!

在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;
}

在每个ViewController中(如果您想更改未选择的图片颜色)

- (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];
}

此代码的线索是'UIImageRenderingModeAlwaysOriginal':

Apple文档的渲染模式:

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

答案 1 :(得分:0)

UITabBarItem 从不拥有tint属性。 没有内置类曾经拥有tint属性。这里没有任何改变。

在iOS 7和7.1中,UITabBar具有tintColor属性,因为它是UIView。

答案 2 :(得分:-1)

选择时,这会更改图像和标签的色调。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
}