加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 如何更改默认蓝色的tabbar图标颜色?

发布时间:2020-12-14 19:55:22 所属栏目:百科 来源:网络整理
导读:我有四个标签.我能够将标签图标颜色从默认的蓝色更改为红色(或者可能是任何颜色),并且它可以完美地运行.问题是它只适用于三个tabbaritems,最后一个默认为蓝色.以下是代码.我在rootviewcontrollerAppDelegate.m中对此进行编码您可以通过在appdelegate中粘贴以
我有四个标签.我能够将标签图标颜色从默认的蓝色更改为红色(或者可能是任何颜色),并且它可以完美地运行.问题是它只适用于三个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;
}

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读