Home > CodeSnippets, Development, iOS > UINavigationBar Color of More Tab in iOS 5

UINavigationBar Color of More Tab in iOS 5

If you start a Tabbed Application and use Storyboards in Xcode with iOS 5 a simple project will be created with two tabs. If you add more than 5 Tabs to the Tab Bar Controller a “More” tab will be created automatically.

The NavigationBar in the More Tab and the modal view (edit) are colored with default blue. To change the color of the moreNavigationController you have to do the following steps.

Extend app delegate so that it implements the UIApplicationDelegate and UITabBarControllerDelegate protocols.


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> {

 UIWindow *window;
 UITabBarController *tabBarController;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;

@end

The app delegate doesn’t know anything about the ViewControllers yet, so it will have to dig through the storyboard to find it. With Interface Builder you always had a reference to the App Delegate in your MainWindow.xib and you could make connections from your top-level view controllers to outlets on the App Delegate. That is currently not possible with storyboards. You cannot make references to the app delegate from your top-level view controllers. So you have to get those references programmatically. You know that the storyboard’s initial view controller is a Tab Bar Controller, so you can look up the window’s rootViewController and cast it.

UITabBarController *tabBarController = (UITabBarController *)
  self.window.rootViewController;

Then you can get the More Tab by the Tab Bar Controller and  set a color.


UINavigationController *moreController = tabBarController.moreNavigationController;
moreController.navigationBar.tintColor = [UIColor orangeColor];

After that you use the App Delegate as the delegate of the Tab Bar and change the color of the “Edit” View, too.

tabBarController.delegate = self;
...</pre>
UIView *editView = [controller.view.subviews objectAtIndex:1];
 UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];
 modalNavBar.tintColor = [UIColor orangeColor];
<pre>

The complete code of AppDelegate.m:


#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 // Get top-level view controller reference programmatically
 tabBarController = (UITabBarController *) self.window.rootViewController;

 // Set the color of the navigationbar in moretab
 UINavigationController *moreController = tabBarController.moreNavigationController;
 // //moreController.navigationBar.barStyle = UIBarStyleBlackOpaque;
 moreController.navigationBar.tintColor = [UIColor orangeColor];
 moreController.navigationBar.hidden = NO;
 moreController.navigationBar.backgroundColor = [UIColor blackColor];

 // Set delegate of the tabBarController to handle the UITabBarControllerDelegate calls
 tabBarController.delegate = self;

 return YES;
}

- (void) tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {

 // Set the color of the navigationbar if edit was selected
 UIView *editView = [controller.view.subviews objectAtIndex:1];
 UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];
 modalNavBar.tintColor = [UIColor orangeColor];
}

@end

Here you can download the example Project.

Advertisements
Categories: CodeSnippets, Development, iOS
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: