7

我有四个标签。我能够将标签图标颜色从默认的蓝色更改为红色(或者可能是任何颜色),并且效果很好。问题是它只适用于三个 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;
}

在此处输入图像描述

4

5 回答 5

8
 [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
于 2013-02-25T03:53:49.293 回答
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);

如果我错了,请免费告诉我:)

于 2011-06-01T03:58:55.243 回答
2

自添加标签栏项目没问题,我测试了这个代码的 4 个项目;

但是您的最后一个标签栏项目是系统标签栏项目(“....”“更多”项目),因此此代码可能没有用;它只是不使用您设置的图像;

于 2011-05-18T06:26:22.153 回答
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
于 2012-11-09T11:38:28.203 回答
0

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

在此处输入图像描述

于 2016-01-26T12:57:32.673 回答