好的,我知道已经有关于此的线索,但是它们中的任何一个似乎对我都有用,我的具体情况。
我有一个标签栏5个项目,我希望中心项目(索引2)在UNSELECTED时是不同的颜色。我不在乎文本是原始颜色,也不关心选择它时的颜色。
这是扭曲;我正在使用“图像”资源为标签栏提供图标,似乎如果我不使用资产,我的所有标签栏图标都会像素化。另外,在创建标签栏控制器时,它说使用图像资源。
任何人都可以对我有所了解吗?谢谢。
答案 0 :(得分: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;
}
因此,每个按钮都可以独立设置。你们每个按钮都是setUp with setColor:forState:UIControlStateNormal和setColor:forState:UIControlStateSelected
瞧! (适用于iOS 7分钟)
答案 1 :(得分:1)
将它放在ViewController的viewDidLoad
:
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1 green:1 blue:1 alpha:1]
} forState:UIControlStateNormal];