One of the common questions we receive is how to update the Navigation Bar, especially as Dapp will create a separate navigation bar for us but will not update the Navigation Controllers Navigation Bar provided by the iPhone SDK.

A common export of the navigation bar might look as follows –


- (void)loadnav {
UINavigationBar *nav = [[UINavigationBar alloc] init];
[nav setBarStyle:UIBarStyleBlack];
[nav setTranslucent:YES];
[nav setTintColor:[UIColor redColor]];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Done"];
[nav setItems:[NSArray arrayWithObject:navigationItem]];
[navigationItem release];
[nav setFrame:CGRectMake(0, -44, 320, 44)];
[self addSubview:nav];

The navigation bars y co-ordinates are -44, placing it behind the Navigation Controller that the iPhone SDK inserts. We want to take the attributes from this navigation bar and update the navigation controller with them.

Attributes like bar style, translucent and tintColor need to be assigned in our App Delegate, before the Navigation Controller is loaded. This is because after the Nav Controller is loaded, these attributes will become read-only.

So, after exporting a project, open up the AppDelegate file and insert the following code after the navigation controller is initialised.

<pre lang=”ObjC”>// Initialise the navigation controller with the first view controller as its root view controller
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

// Add your code here
[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
// I have commented out the set translucent attribute, as it has some interesting effects. Try uncommenting it :).
// [navigationController.navigationBar setTranslucent:YES];
[navigationController.navigationBar setTintColor:[UIColor redColor]];

// If you wish to hide the navigation controller
// Note: We are able to alternatively hide or show the navigation bar by using
// [self.navigationController setNavigationBarHidden:YES] in our viewWillAppear method
// within the specific UIViewController that we wish to hide/show the navigation bar
[navigationController setNavigationBarHidden:YES];

// Navigation controller has copy of view controller, so release our copy
[rootViewController release];</pre>

Luckily, updating the title is much easier. Apple provide a convenience method for this, in any View Controller (usually in the init method) you can simply type –


self.title = @"My Title";

The final item that you might want to add to a navigation bar is a bar button item. Again, in the init method of a view controller a sample bar button item you can create is:


UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Info"
[self.navigationItem setRightBarButtonItem:infoButtonItem];

The action section of the bar button item tells us what method is called when the user presses the button, to handle this call we need to create the method within our code.


- (void)infoButtonAction {
// Put your own code in here
NSLog(@"User pressed the info button");