xcode 如何将标签栏图标颜色从默认蓝色更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5799572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:55:16  来源:igfitidea点击:

How to change tabbar icon color from default blue?

objective-cxcodeuitabbarcontrolleruitabbaritem

提问by kingston

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.mYou could try this by pasting the below code in your appdelegate. Could you guys help me out I'd be so greatful!

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

在此处输入图片说明

采纳答案by canny

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

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

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;

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

回答by codercat

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

回答by mcsquare

Thanks for your sharing.

感谢您的分享。

But there are some flaws where deploying on iPhone4 or iPod4 which have retina display. The selected icon in the tarBar will be smaller than the unselected one.

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

So I would like to share my fix here:

所以我想在这里分享我的修复:

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);

If I am wrong, please free fee to let me know :)

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

回答by kalpesh jetani

@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

回答by HannahCarney

go to your asset folder, find the asset and click on Identity Inspector , and change "Render As" to Original Image

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

enter image description here

在此处输入图片说明