如何更改默认蓝色的标签栏图标颜色?

时间:2011-04-27 05:01:49

标签: objective-c xcode uitabbarcontroller uitabbaritem

我有四个标签。我能够将标签图标颜色从默认的蓝色更改为红色(或者可能是任何颜色),并且它可以完美地运行。问题是它只适用于三个tabbaritems,最后一个默认为蓝色。下面是代码。我在rootviewcontrollerAppDelegate.m中对此进行编码您可以通过在appdelegate中粘贴以下代码来尝试此操作。你们能帮助我,我会非常感激!

@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]);
            // 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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions 
{    

    [[tabBarController tabBar] recolorItemsWithColor:[UIColor redColor] shadowColor:[UIColor blackColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f];

    [self.window addSubview:tabBarController.view];

        [self.window makeKeyAndVisible];

        [self addTabBarArrow];

         return YES;
}

enter image description here

5 个答案:

答案 0 :(得分:8)

 [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];

答案 1 :(得分:3)

感谢您的分享。

但是在iPhone4或iPod4上部署视网膜显示器存在一些缺陷。 tarBar中的所选图标将小于未选中的图标。

所以我想在这里分享我的解决方案:

CGSize orginalSize = [[item selectedImage] size];
double scaleFactor = 1;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    scaleFactor = [[UIScreen mainScreen] scale];
}
    contextRect.size = CGSizeMake(orginalSize.width*scaleFactor, orginalSize.height*scaleFactor);

// Retrieve source image and begin image context
UIImage *itemImage = [item image];
double imageScale = 1;
if ([itemImage respondsToSelector:@selector(scale)]) {
    imageScale = itemImage.scale;
}
CGSize itemImageSize = CGSizeMake(itemImage.size.width*imageScale, itemImage.size.height*imageScale);

如果我错了,请免费让我知道:)

答案 2 :(得分:2)

自我添加tabbar-item没问题,我测试了4个项目的代码;

但是你的上一个tabbar项是一个系统tabbar项(“....”“more”项),所以这段代码可能没用;它只是没有使用你的设置图像;

答案 3 :(得分:0)

@implementation MoreViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = @"More";
        self.tabBarItem.image=[UIImage imageNamed:@"more.png"]; // here more.png is Yellow Image
    }
    return self;
}

//.......
@end

答案 4 :(得分:0)

转到资产文件夹,找到资产并单击Identity Inspector,然后将“Render As”更改为原始图像

enter image description here