繁体   English   中英

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

[英]Change Tab Bar Item Colour iOS7?

好的,我知道已经有关于此的线程,但是似乎没有一个对我有用,对于我的特殊情况也是如此。

我有一个选项卡栏5个项目,并且我希望中心项目(索引2)在取消选择时具有不同的颜色。 我不在乎文本是否是原始颜色,也不必关心选择的颜色。

这是转折点; 我正在使用“图像”资产来为标签栏提供图标,如果我不使用资产,似乎所有标签栏图标都已像素化。 另外,在创建标签栏控制器时,它说使用图像资产。

谁能给我一些启示? 谢谢。

我最终要做的是:创建了一个自定义UITabBarItem类。

@interface MyTabBarItem : UITabBarItem

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

@end

它的实现:

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

因此,每个按钮都可以独立设置。 每个按钮都使用setColor:forState:UIControlStateNormal和setColor:forState:UIControlStateSelected设置

和瞧! (适用于iOS 7分钟)

把它放在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