UPDATING UINAVIGATIONCONTROLLER NAVIGATION BAR TUTORIAL

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"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(infoButtonAction)];
[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");
}

 

6 thoughts on “UPDATING UINAVIGATIONCONTROLLER NAVIGATION BAR TUTORIAL”

  • I’m having a big problem with Dap 1.2.3 –
    When I select the “Show Left Button” “Show Right Button” and “Editing Mode” to ON , they will not stay set to ON. As soon as I move to another opition, it resets to OFF. So whien I’m in the VIEW MODE, the Left and Right buttons do not dislay.

    I had the same problem in the previous version but thought the upgrade would correct it. Please explain how to fix this.
    Thanks

    • I am still unable to reproduce this issue. When conducting tests, all Navigation Bar settings work correctly. I have tested this on an iPhone 3G, iPhone 3GS and on the Xcode simulator running iPhone 4 with latest OS.

      We have an iPhone 4 on order and I hope that once it arrives I am able to reproduce the issue on that physical device.

      • Just picked up an iPhone 4 and I can now reproduce the issue :). It seems to be affecting any ‘switch’ boolean settings. I will be releasing a patch to correct this shortly.

    • You want to access the tintColor property of the navigation bar to do this.

      Generally you’ll want to do this before the navigation controller (which hosts the navigation bar) is added to the view.

      So, you’d be looking at using for example – navigationController.navigationBar.tintColor = [UIColor greenColor];

      Although, looks like you found the answer anyways :). Good ‘ol google.

Leave a Reply

Your email address will not be published. Required fields are marked *