【问题标题】:How to change tabbar icon color from default blue?如何从默认的蓝色更改标签栏图标颜色?
【发布时间】:2011-04-27 05:01:49
【问题描述】:

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

【问题讨论】:

    标签: objective-c xcode uitabbarcontroller uitabbaritem


    【解决方案1】:
     [[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
    

    【讨论】:

      【解决方案2】:

      感谢您的分享。

      但在具有视网膜显示屏的 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);
      

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 谢谢人...我遇到了另一个问题..上面的代码只适用于一个应用程序..我在其他应用程序中尝试了相同的代码它可以工作...你试试这个stuff.probably..in some other application...然后回复我
        【解决方案4】:
        @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
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2012-01-15
            • 1970-01-01
            • 2018-07-04
            • 1970-01-01
            • 2012-05-21
            • 2013-06-11
            • 2012-01-26
            • 1970-01-01
            相关资源
            最近更新 更多