2012-04-25 74 views
2

如何改變此代碼中的標籤欄顏色?我的標籤欄是黑色的。點擊後將其更改爲藍色。我想改變任何其他顏色的拖車顏色。如何更改Xcode中的標籤欄顏色

此代碼是:application.m 有:rotatingTabBarController.m,rotatingTabBarController.h

// 1B) If this app uses tabs, create a tabbed navigation application and set the default screen for each tab 
//  to the defaultScreenGuid in the tabs data 
if([self.tabs count] > 0){ 
    [BT_debugger showIt:self:[NSString stringWithFormat:@"building a tabbed based navigation app%@", @""]]; 

    if([self.screens count] > 0){ 

     //appDelegate 
     qoqb_appDelegate *appDelegate = (qoqb_appDelegate *)[[UIApplication sharedApplication] delegate]; 

     //initialize the tab bar controller 
     rootTabBarController = [[BT_rotatingTabBarController alloc] init]; 
     [rootTabBarController.view setFrame:[[UIScreen mainScreen] bounds]]; 
     [rootTabBarController setDelegate:appDelegate]; 
     rootTabBarController.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

     //if we have a tabbar color setup in the theme 
     if(self.rootTheme != nil){ 
      if([self.rootTheme.jsonVars objectForKey:@"tabBarColor"]){ 
       if([[self.rootTheme.jsonVars objectForKey:@"tabBarColor"] length] > 3){ 
        UIColor *tabberColor = [BT_color getColorFromHexString:[self.rootTheme.jsonVars objectForKey:@"tabBarColor"]]; 
        NSString *tabberOpacity = @".50"; 
        if([self.rootTheme.jsonVars objectForKey:@"tabBarColorOpacity"]){ 
         if([[self.rootTheme.jsonVars objectForKey:@"tabBarColorOpacity"] length] > 0){ 
          tabberOpacity = [NSString stringWithFormat:@".%@", [self.rootTheme.jsonVars objectForKey:@"tabBarColorOpacity"]]; 
          if([tabberOpacity isEqualToString:@".100"]) tabberOpacity = @".50"; 
         } 
         //colorize the tab-bar 
         [rootTabBarController addTabColor:tabberColor:[tabberOpacity doubleValue]]; 
        } 
       } 
      } 
     } 

     //Fill a temporary array of view controllers to assign to tab bar controller 
     NSMutableArray *tmpViewControllers = [[NSMutableArray alloc] init]; 

     //loop through each tab bar item in application data 
     for(int i = 0; i < [[self tabs] count]; i++){ 

      //this tab 
      BT_item *thisTab = (BT_item *)[[self tabs] objectAtIndex:i]; 
      NSString *textLabel = [[thisTab jsonVars] objectForKey:@"textLabel"];    
      UIImage *tabIcon = [UIImage imageNamed:[[thisTab jsonVars] objectForKey:@"iconName"]];    

      //get the screen from the apps array of screens for this tab's view controller 
      if([[thisTab jsonVars] objectForKey:@"homeScreenItemId"]){ 

       BT_item *thisTabsDefaultScreenData = [self getScreenDataByItemId:[[thisTab jsonVars] objectForKey:@"homeScreenItemId"]]; 

       //if this is the first tab in the list, remember it as the "currently loaded screen", also make it the "previously loaded screen" 
       if(i == 0){ 
        [self setCurrentScreenData:thisTabsDefaultScreenData]; 
        [self setPreviousScreenData:thisTabsDefaultScreenData]; 
        [thisTabsDefaultScreenData setIsHomeScreen:TRUE]; 

        //if theScreen has an audio file..load it in the delegate 
        if([[BT_strings getJsonPropertyValue:thisTabsDefaultScreenData.jsonVars:@"audioFileName":@""] length] > 3){ 

         //appDelegate 
         qoqb_appDelegate *appDelegate = (qoqb_appDelegate *)[[UIApplication sharedApplication] delegate]; 

         //initialize audio in different thread to prevent UI blocking 
         [NSThread detachNewThreadSelector: @selector(loadAudioForScreen:) toTarget:appDelegate withObject:thisTabsDefaultScreenData]; 

        }      

       } 

回答

6
tabBar.tintColor = [UIColor greenColor]; 

將崩潰,如果你的IOS版本不是IOS 5看到docs

較安全的方法是:

if ([tabBarController.tabBar respondsToSelector:@selector(setTintColor:)]) { 
    [tabBarController.tabBar setTintColor:color]; 
} 

// NEW CODE

if ([rootTabBarController.tabBar respondsToSelector:@selector(setTintColor:)]) { 
    [rootTabBarController.tabBar setTintColor:tabberColor]; 
} 
+0

我使用最新的Xcode與ios5模擬...我試圖把代碼之前和之後,我得到錯誤 – 2012-04-25 08:46:01

+0

你會得到什麼樣的錯誤? – TompaLompa 2012-04-25 08:56:26

+0

我使錯誤截圖:http://imageupper.com/ s02/1/7/O13353450461627064_1.jpg – 2012-04-25 09:12:16

0
tabBar.tintColor = [UIColor greenColor]; 
+0

和我需要把代碼? – 2012-04-25 07:13:35

+0

當需要改變標籤欄顏色。在你的代碼我想在這行....「[rootTabBarController addTabColor:tabberColor:[tabberOpacity doubleValue]];' – Nit 2012-04-25 07:17:14

+0

它不工作...我得到紅色警告,當我嘗試它:( – 2012-04-25 07:40:23

0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
self.tabBarController.tabBar.tintColor=[UIColor redColor]; 
return YES; 

} 

在AppDelegate.m

1

在AppDelegate.m在裏面:application didFinishLaunchingWithOptions:

使用此編碼:

// use this for highlighted color 
[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:(256/256.0) green:(0/256.0) blue:(0/256.0) alpha:(1.0)]]; 

// Use this to channge the tint colo of the entere TabBar 
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:(255/256.0) green:(0/256.0) blue:(0/256.0) alpha:(1.0)]]; 

// This change only a background color 
[[UITabBar appearance] setBackgroundColor:[UIColor colorWithRed:(255/256.0) green:(255/256.0) blue:(255/256.0) alpha:1.0]]; 

// This for change IMG or color tint on back of highlighted button. 
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"YourIMG.png"]]; 

這就是它這一切;)

0

在IOS7,這是不同的做法:

[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 

您可以按照下列步驟操作:

我創建了一個新的UINavigationController例如UIDemoNavController導致:

- (void)viewDidLoad{ 
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 
    [super viewDidLoad]; 
} 

這是完整的演示課程:

#import "UIDemoNavController.h" 

@interface UIDemoNavController() 

@end 

@implementation UIDemoNavController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) {} 
    return self; 
} 

- (void)viewDidLoad{ 
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]]; 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning{ 
    [super didReceiveMemoryWarning]; 
} 

@end 
相關問題