How to Change Color of Navigation Bar in iPhone

Introduction

In this article we create an empty view application and use a uiviewcontroller class to navigate from one view to another. For changing the color of the navigation bar we use code in the view load method of each view. In this I use two views, FirstViewController and SecondViewController.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select Empty View Application and click on Next.

Step 4

Now provide your Product Name, Company Identifier  and click on Next.

Step 5

Select the location where you want to save your project to and click on Create.

Step 6

Now you will see in the XCode Window two Objective-C Classes that were generated by XCode; they are:

1. Appdelegate.h
2. AppDelegate.m

Now we add two UIViewControllers; for this we use the following procedure.

Step 7  

Select the appdelegate.m class and right-click on it, as in:

choose-new-file-in-iPhone.png

Choose "New File".

Step 8

Select "Subclass of UIViewController" from the drop down list and give a class name. Here I use "First view controller" for the class name. Then click on the checkbox with the xib use interface. Add a label and button from xib.

first-class-name-in-iPhone.png

Click on Next.

Step 9

Now click on "Create" to import a class into the project, as in:

select-class-target-in-iPhone.png

Repeat the steps to add the second view controller class.

second-class-name-in-iPhone.png

Step 10

After that we code for navigation and to do the linking.

select-button-action-touch-up-inside-in-iPhone.png

Step 11

To change the color of the navigation bar we use the following code in the appdelegate.m Objective-C file:

#import "AppDelegate.h"

@implementation AppDelegate
@synthesize window = _window;
@synthesize nav,first;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    first =[[FirstViewController alloc]init];
    self.nav=[[UINavigationController alloc]initWithRootViewController:first];
    // Override point for customization after application launch.
    self.window.rootViewController = self.nav;
   nav.navigationBar.tintColor=[UIColor colorWithRed:42/256.0 green:60/256.0 blue:80/256.0 alpha:1.0];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
@end

Step 12

Now select which platform you want to see output for.

run-in-iPhone.png

Output

Output 1 in iPhone:

Outpu1-in-iPhone.png

Output 2 in iPhone:

Output2-in-iPhone.png

Up Next
    Ebook Download
    View all
    Learn
    View all