2011-04-27 1 views
7

4 개의 탭이 있습니다. 탭 아이콘 색상을 기본 파란색에서 빨간색 (또는 아마 모든 색상)으로 변경할 수 있었으며 완벽하게 정상적으로 작동합니다. 문제는 3 개의 tabbaritem에 대해서만 작동하며 마지막 하나는 기본 파란색입니다. 아래는 코드입니다. 나는 이것을 다음과 같이 코딩하고있다 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 개 항목이 코드를 테스트;

하지만 마지막 탭 모음 항목은 시스템 탭 모음 항목 ("...." "more"항목)이므로이 코드는 사용하지 못할 수도 있습니다. 그것의 다만 당신의 세트를 안으로 사용하지 않는 심상;

+0

감사합니다 ... 나는 다른 문제에 봉착했습니다. 위의 코드는 단지 하나의 응용 프로그램에서만 작동했습니다. 다른 응용 프로그램에서 같은 코드를 시도했는데 ... cud u가이 물건을 시도합니다. 아마도 ... 다른 응용 프로그램에서 ... 그리고 나에게 답장해라. – kingston

3

공유해 주셔서 감사합니다.

하지만 망막 디스플레이가있는 iPhone4 또는 iPod4에 배포 할 때 몇 가지 결함이 있습니다. tarBar에서 선택된 아이콘은 선택되지 않은 아이콘보다 작습니다.

그래서 여기 내 수정 프로그램을 공유하고 싶습니다 : 내가 잘못 있으면 알려 주시기하기 :

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

당신의 자산 폴더로 이동 무료 수수료를 기쁘게

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

애셋을 찾은 다음 ID 관리자를 클릭하고 "다른 이름으로 렌더링"을 원래 이미지로 변경하십시오.

enter image description here