本文介绍了仅更改一个特定的UITabBarItem色调颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 众所周知,UITabBarController中所选(或活动)项目的色调颜色可以轻松更改,下面是一个示例:It is well known that the tint color of selected (or active) items in a UITabBarController can be easily changed, here is an example:myBarController.tabBar.tintColor = [UIColor redColor];在这个例子中,tabBar中的任何标签栏项目都会显示红色一旦它被激活就会着色。同样,这适用于此标签栏中项目的所有。In this instance, any tab bar item in tabBar will have a red tint once it is made active. Again, this applies to all of the items in this tab bar.如何使用其他标签栏项目之间的活动色调颜色不同同一个酒吧?例如,一个项目在选中时可能具有红色,而另一个项目可能具有蓝色色调。How can the active tint color be different between other tab bar items in the same bar? For example, one item might have a red tint while selected, while another might have a blue tint.我知道这可以通过重绘和子类化来解决整个标签栏。然而,这是我需要的唯一改变,这样做似乎有点过头了。我不是试图改变样式或以任何方式呈现项目的方式,只是为了使不同项目之间的风格不同。I am aware that this can probably be solved by redrawing and subclassing the entire tab bar. However, this is the only change I need, and it seems overkill to do so. I'm not trying to change the style or how the items are rendered in any way, just to make that style different between different items.我还没有看到任何任何与iOS 7和8中的更新相关的问题的答案。I haven't seen any answers to this question anywhere that are relevant to the updates in iOS 7 and 8.推荐答案我做了一些实验并基于这个答案,找到了一种方法来做我想要的而不用继承UITabBarItem或UITabBar!I did some experimenting and based on this answer, found a way to do what I want without subclassing UITabBarItem or UITabBar!基本上,我们的想法是创建一个UIImage方法,模仿UITabBar的色调遮罩行为,同时以原始形式渲染它并避免使用本机色调遮罩。Basically, the idea is to create a method of UIImage that mimics the tint mask behavior of UITabBar, while rendering it in its "original" form and avoiding the native tint mask.你所要做的就是创建一个新的UIImage实例方法,它返回一个用我们想要的颜色掩盖的图像:All you have to do is create a new instance method of UIImage that returns an image masked with the color we want:@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这是一个相当简单的代码版本我发布的答案有一个例外 - 返回的图像将其渲染模式设置为始终原始,这样可确保不会应用默认的UITabBar掩码。现在,只需在编辑标签栏项时使用此方法:This is a fairly straightforward version of the code in the answer that I posted, with one exception- the returned image has its rendering mode set to always original, which makes sure that the default UITabBar mask won't be applied. Now, all that is needed is to use this method when editing the tab bar item:navController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"title" image:normal_image selectedImage:[selected_image tabBarImageWithCustomTint:[UIColor redColor]]];毋庸置疑, selected_image 是正常的图像一来自 UIImage imageNamed:而 [UIColor redColor 可以替换为任何一种颜色。Needless to say, selected_image is the normal image one gets from UIImage imageNamed: and the [UIColor redColor can be replaced with any color one desires. 这篇关于仅更改一个特定的UITabBarItem色调颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 00:28