简体   繁体   English

更改选项卡栏项目颜色iOS7?

[英]Change Tab Bar Item Colour iOS7?

Okay, I know there are already threads about this, but NONE of them seem to work for me, and my particular case. 好的,我知道已经有关于此的线程,但是似乎没有一个对我有用,对于我的特殊情况也是如此。

I have a tab bar 5 items, and I want the central item (index 2) to be a different colour when UNSELECTED. 我有一个选项卡栏5个项目,并且我希望中心项目(索引2)在取消选择时具有不同的颜色。 I don't care if the text is original colour, nor do I care about the colour when it is selected. 我不在乎文本是否是原始颜色,也不必关心选择的颜色。

Here is the twist; 这是转折点; I am using the 'Images' assets to provide the icon for the tab bar, it seems if I don't use the assets all my tab bar icons are pixelated. 我正在使用“图像”资产来为标签栏提供图标,如果我不使用资产,似乎所有标签栏图标都已像素化。 Plus, when creating the tab bar controller, it said to use image assets. 另外,在创建标签栏控制器时,它说使用图像资产。

Can anyone shed some light on me? 谁能给我一些启示? Thank you. 谢谢。

What I ended up doing : I created a custom UITabBarItem class. 我最终要做的是:创建了一个自定义UITabBarItem类。

@interface MyTabBarItem : UITabBarItem

- (void)setColor:(UIColor *)color forState:(UIControlState)state;

@end

And it's implementation : 它的实现:

- (void)setColor:(UIColor *)color forState:(UIControlState)state {
    NSMutableDictionary *attributes = [[self titleTextAttributesForState:state] mutableCopy];
    if (!attributes) {
        attributes = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    attributes[NSForegroundColorAttributeName] = color;
    [self setTitleTextAttributes:attributes forState:state];

    UIImage *image = [[self tintImage:self.image WithColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (state == UIControlStateSelected) {
        self.selectedImage = image;
    } else {
        self.image = image;
    }
}

- (UIImage *)tintImage:(UIImage *)image WithColor:(UIColor *)tintColor {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
    CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height);
    [image drawInRect:drawRect];
    [tintColor set];
    UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tintedImage;
}

So, each button can be set up independently. 因此,每个按钮都可以独立设置。 Each of you button is setUp with setColor:forState:UIControlStateNormal and setColor:forState:UIControlStateSelected 每个按钮都使用setColor:forState:UIControlStateNormal和setColor:forState:UIControlStateSelected设置

And voilà! 和瞧! (Works for iOS 7 min) (适用于iOS 7分钟)

Put this in your ViewController's viewDidLoad : 把它放在ViewController的viewDidLoad

[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1 green:1 blue:1 alpha:1]
                                                } forState:UIControlStateNormal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM