iOS TabBar图像分配

如果可能的话,你将如何去设置一个自定义图像到每个标签栏项目的状态(选中/未选中)。 这将需要覆盖选中时标签栏项图像蓝色的默认行为,并在未选中时对其进行灰色处理。 谢谢。

要重新着色选项卡栏项目使用 –

/* for colored tab items */ @interface UITabBar (ColorExtensions) - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur; @end @interface UITabBarItem (Private) @property(retain, nonatomic) UIImage *selectedImage; - (void)_updateView; @end @implementation UITabBar (ColorExtensions) - (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur { CGColorRef cgColor = [color CGColor]; CGColorRef cgShadowColor = [shadowColor CGColor]; for (UITabBarItem *item in [self items]) if ([item respondsToSelector:@selector(selectedImage)] && [item respondsToSelector:@selector(setSelectedImage:)] && [item respondsToSelector:@selector(_updateView)]) { CGRect contextRect; contextRect.origin.x = 0.0f; contextRect.origin.y = 0.0f; contextRect.size = [[item selectedImage] size]; // Retrieve source image and begin image context UIImage *itemImage = [item image]; CGSize itemImageSize = [itemImage size]; CGPoint itemImagePosition; itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2); itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2); UIGraphicsBeginImageContext(contextRect.size); CGContextRef c = UIGraphicsGetCurrentContext(); // Setup shadow CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor); // Setup transparency layer and clip to mask CGContextBeginTransparencyLayer(c, NULL); CGContextScaleCTM(c, 1.0, -1.0); CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]); /* //Setup the gradient... CGFloat components[8] = {0.0,0.4,1.0,0.2,0.0,0.6,1.0,1.0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2); CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(0,contextRect.size.height),0); */ // Fill and end the transparency layer CGContextSetFillColorWithColor(c, cgColor); contextRect.size.height = -contextRect.size.height; CGContextFillRect(c, contextRect); CGContextEndTransparencyLayer(c); // Set selected image and end context [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()]; UIGraphicsEndImageContext(); // Update the view [item _updateView]; } } @end /* colored tab items code completed */ 

然后在应用程序委托使用 –

 [[tabbarcontroller tabBar] recolorItemsWithColor:[UIColor colorWithRed:0.6640 green:0.1992 blue:0.1992 alpha:1.0] shadowColor:[UIColor clearColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f]; 

你的问题听起来像你试图改变标签栏的select颜色。 这并不容易,但可以做到。 这里有一些关于它的讨论的链接

在UITabBar中自定义颜色

http://didikot.com/?p=106

谷歌search将带来其他人如果这些没有帮助。