ele*_*119 7 uitabbaritem ios ios7 ios8
众所周知,UITabBarController中所选(或活动)项的色调颜色可以很容易地改变,这是一个例子:
myBarController.tabBar.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)
在这种情况下,tabBar中的任何标签栏项目一旦激活就会有红色.同样,这适用于此标签栏中的所有项目.
如何在同一个栏中的其他标签栏项目之间显示活动色调颜色?例如,一个项目在选中时可能具有红色,而另一个项目可能具有蓝色色调.
我知道这可以通过重绘和子类化整个标签栏来解决.然而,这是我需要的唯一改变,这样做似乎有点过头了.我不是试图改变样式或如何以任何方式呈现项目,只是为了使不同项目之间的风格不同.
我没有看到任何与iOS 7和8中的更新相关的问题的答案.
有一个更简单的方法来做到这一点!将其添加到ViewController中,UITabBar项应该是另一种颜色
- (void) viewWillAppear:(BOOL)animated {
// change tint color to red
[self.tabBarController.tabBar setTintColor:[UIColor redColor]];
[super viewWillAppear: animated];
}
Run Code Online (Sandbox Code Playgroud)
将其插入其他ViewControllers
- (void) viewWillAppear:(BOOL)animated {
// change tint color to black
[self.tabBarController.tabBar setTintColor:[UIColor blackColor]];
[super viewWillAppear: animated];
}
Run Code Online (Sandbox Code Playgroud)
我使用它来在每个ViewController中获得不同的Tint颜色,例如:[red | 黑色| 绿色| 粉色]
我做了一些实验,并根据这个答案,找到了一种方法可以做我想做的事情,而无需子类化 UITabBarItem 或 UITabBar!
基本上,这个想法是创建一个 UIImage 的方法来模仿 UITabBar 的色调遮罩行为,同时以其“原始”形式渲染它并避免使用本机色调遮罩。
您所要做的就是创建一个新的 UIImage 实例方法,该方法返回用我们想要的颜色遮盖的图像:
@interface UIImage(Overlay)
- (instancetype)tabBarImageWithCustomTint:(UIColor *)tintColor;
@end
@implementation UIImage(Overlay)
- (instancetype)tabBarImageWithCustomTint:(UIColor *)tintColor
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[tintColor setFill];
CGContextFillRect(context, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
return newImage;
}
@end
Run Code Online (Sandbox Code Playgroud)
这是我发布的答案中代码的相当简单的版本,但有一个例外 - 返回的图像将其渲染模式设置为始终原始,这确保不会应用默认的 UITabBar 掩码。现在,所需要做的就是在编辑选项卡栏项目时使用此方法:
navController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"title" image:normal_image selectedImage:[selected_image tabBarImageWithCustomTint:[UIColor redColor]]];
Run Code Online (Sandbox Code Playgroud)
不用说,selected_image这是人们获得的正常图像UIImage imageNamed:,并且[UIColor redColor可以用人们想要的任何颜色替换。
| 归档时间: |
|
| 查看次数: |
2590 次 |
| 最近记录: |