2

How do I go about changing the tab bar color in this code? My tab bar is black. On click changes it to blue. I want to change the tow color to any other color.

This code is from :application.m and there :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];

                    }                       

                }   
Linger
  • 14,942
  • 23
  • 52
  • 79
Moh ali
  • 31
  • 1
  • 1
  • 6

5 Answers5

6
tabBar.tintColor = [UIColor greenColor];

will crash if your IOS version is not IOS 5 see the docs

a safer approach is:

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

//NEW CODE

if ([rootTabBarController.tabBar respondsToSelector:@selector(setTintColor:)]) {
    [rootTabBarController.tabBar setTintColor:tabberColor];
}
TompaLompa
  • 949
  • 6
  • 17
  • i use latest xcode with ios5 simulation... i try to put the code before and after and i get error – Moh ali Apr 25 '12 at 08:46
  • i make screenshot for the error at :http://imageupper.com/s02/1/7/O13353450461627064_1.jpg – Moh ali Apr 25 '12 at 09:12
  • added an update. that should work. You need to remove the line: //colorize the tab-bar [rootTabBarController addTabColor:tabberColor:[tabberOpacity doubleValue]]; – TompaLompa Apr 25 '12 at 09:18
  • now i get one error only ..this is the screenshot:http://imageupper.com/s03/1/2/R13353462121628011_1.jpg – Moh ali Apr 25 '12 at 09:31
  • thank you alot ...now there is no error ..but how can i change the color ...it is still black on my ios simulator – Moh ali Apr 25 '12 at 09:41
  • you loaded your color from :: [BT_color getColorFromHexString:[self.rootTheme.jsonVars objectForKey:@"tabBarColor"]] maybe something goes wrong there. You can always check with setting tabberColor = [UIColor greenColor]; This should make the tabBar green – TompaLompa Apr 25 '12 at 09:47
  • thank you alot tompalompa you help me ..i will try it and i hope it's get green :)... do i need to put tabberColor = [UIColor greenColor]; in same place ? – Moh ali Apr 25 '12 at 10:01
  • do i need to remove: BT_color getColorFromHexString:[self.rootTheme.jsonVars objectForKey:@"tabBarColor"] and how i mark your answer as great here do i need to click any thing ? – Moh ali Apr 25 '12 at 10:25
  • You could as it would look cleaner. but you dont need to. Just mark answer as right – TompaLompa Apr 25 '12 at 10:31
1

In the AppDelegate.m inside at: application didFinishLaunchingWithOptions:

Using this coding:

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

That's it that's all ;)

BlackSheep
  • 1,087
  • 12
  • 29
0
tabBar.tintColor = [UIColor greenColor];
Nitin
  • 7,455
  • 2
  • 32
  • 51
  • when require change the tab bar color.In Your code i think before this line....`[rootTabBarController addTabColor:tabberColor:[tabberOpacity doubleValue]];` – Nitin Apr 25 '12 at 07:17
  • What is name of your tab bar controller.?..You have posted very limited code so i can not find where is your mistake. – Nitin Apr 25 '12 at 07:48
  • i have this file :BT_viewController ,BT_rotatingNavController,BT_rotatingTabBarController,BT_viewControllerManager. ,BT_viewUtilities. ,BT_application – Moh ali Apr 25 '12 at 08:48
0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController.tabBar.tintColor=[UIColor redColor];
return YES;

}

In AppDelegate.m

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
0

In IOS7 this is done differently:

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

You can follow these steps:

I created a new UINavigationController for example UIDemoNavController resulting in:

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

This is the full demo class:

#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
Dalorzo
  • 19,834
  • 7
  • 55
  • 102