2011-05-13 40 views

回答

0

爲了使它成爲你必須覆蓋UITabBarController類,但如果你是新手,我不會做任何努力。在iOS開發中,最好學習iOS guidelines,因爲最終你的應用將被Apple審覈/拒絕審覈。

3

在應用程序委託文件添加此 -

@interface UITabBar (ColorExtensions) 
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur; 
@end 

@interface UITabBarItem (Private) 
@property(retain, nonatomic) UIImage *selectedImage; 
- (void)_updateView; 
@end 

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

//Setup the gradient...  
//CGFloat components[8] = {0.0,0.4,1.0,0.2,0.0,0.6,1.0,1.0}; 
//CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
//CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, NULL, 2); 
//CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(0,contextRect.size.height),0); 


// 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]; 



} 

} 

,然後你可以在應用程序的標籤欄給顏色並完成與此代碼launcing -

[[tabbarcontroller tabBar] recolorItemsWithColor:[UIColor colorWithRed:0.6640 green:0.1992 blue:0.1992 alpha:1.0] shadowColor:[UIColor clearColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f]; 
+0

謝謝您的答覆。此功能也需要實現,但我想更改顯示在標籤欄圖標下的項目中的文本的顏色。這可能嗎? – Paolpa 2011-05-13 10:53:41

+0

謝謝你。它看起來不錯,但我無法讓它工作 - 控制檯提供錯誤,例如'CGContextSetStyle:invalid context 0x0'。有任何想法嗎? – 2013-04-16 08:55:29