如何将标签栏图标颜色从默认蓝色更改? [英] How to change tabbar icon color from default blue?

查看:73
本文介绍了如何将标签栏图标颜色从默认蓝色更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个标签.我能够将选项卡图标颜色从默认的蓝色更改为红色(或可能是任何颜色),并且效果很好.问题是它只适用于三个 tabbaritems,最后一个是默认的蓝色.下面是代码.我在 rootviewcontrollerAppDelegate.m 中编写此代码您可以通过在您的 appdelegate 中粘贴以下代码来尝试此操作.你们能帮帮我吗,我会非常棒的!

I have got four tabs. I was able to change the tab icon color from default blue to red (or probably any color) and it works perfectly fine. The problem is it works only for three tabbaritems and last one is default blue. Below is the code. I'm coding this in rootviewcontrollerAppDelegate.m You could try this by pasting the below code in your appdelegate. Could you guys help me out I'd be so greatful!

@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;
}

推荐答案

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

no problem for self-add tabbar-item, i test this code for 4 items;

但是你的最后一个标签栏项目是一个系统标签栏项目(...."更多"项目),所以这段代码可能没有用;它只是不使用您设置的图像;

but your last tabbar item is a system tabbar item(the"....""more" item), so this code maybe has no use for it; its just not use image your set in;

这篇关于如何将标签栏图标颜色从默认蓝色更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆