2011-04-27 179 views
7

我有四个选项卡。我能够从默认的蓝色标签的图标颜色变为红色(或可能的任何颜色)和它的作品完美的罚款。问题是它只适用于三个tabbaritems,最后一个是默认蓝色。以下是代码。我在rootviewcontrollerAppDelegate.m中编写了这个代码,你可以通过在你的appdelegate中粘贴下面的代码来试试。你们能帮助我吗?我真的太棒了!如何更改默认蓝色的tabbar图标颜色?

@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

回答

2

自我补充的TabBar项目没有问题,我测试此代码为4个项目;

是你的最后的TabBar项目是一个系统的TabBar项目(以下简称“......‘’更多”项),所以这段代码这俩用不到它;它只是不使用你的图像设置;

+0

感谢的人......我已经坚持到上面的代码中的另一个problem..this只是工作只有一个application..i试图在其他应用程序DIS相同的代码,它嚣工作...反刍üstuff.probably试试这个..在其他一些应用程序中...并回复我 – kingston 2011-05-18 07:04:54

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

如果我错了,请免手续费,让我知道:)

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 
8
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]]; 
0

去你的资产文件夹,发现该资产,然后单击身份检查,并更改“渲染为”以原始图像

enter image description here

相关问题