2011-04-27 23 views
7

Tengo cuatro pestañas. Pude cambiar el color del icono de la pestaña del azul predeterminado al rojo (o probablemente a cualquier color) y funciona perfectamente bien. El problema es que funciona solo para tres tabbaritems y el último es azul por defecto. A continuación está el código. Estoy codificando esto en rootviewcontrollerAppDelegate.m Puede intentar esto pegando el código siguiente en su appdelegate. ¿Podrían ayudarme? ¡Sería tan agradecido!¿Cómo cambiar el color del icono tabbar del azul predeterminado?

@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

Respuesta

2

ningún problema para la auto-add-barra de pestañas artículo, i probar este código por 4 artículos;

pero su último elemento tabbar es un elemento tabbar del sistema (el elemento "...." "más"), por lo que este código quizás no tenga uso; simplemente no usa la imagen de su conjunto;

+0

gracias hombre ... me he metido en otro problema ... este código anterior solo funcionó para una sola aplicación ... intenté dis código mismo en otra aplicación que din work ... cud u probar esto stuff.probably .. en alguna otra aplicación ... y respondeme – kingston

3

Gracias por compartir.

Pero hay algunos defectos en la implementación en iPhone4 o iPod4 que tienen pantalla retina. El ícono seleccionado en tarBar será más pequeño que el no seleccionado.

Así que me gustaría compartir mi solucionar aquí:

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

Si estoy equivocado, por favor tasa libre de dejarme saber :)

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

vaya a su carpeta de recursos , encuentre el activo y haga clic en el Inspector de identidad, y cambie "Renderizar como" a la imagen original

enter image description here

Cuestiones relacionadas