Filed under: iPhone
iPhone Coding: Working with the Navigation Bar
The iPhone Navigation Bar offers a simple way to add button-based navigation to your iPhone applications. Defined in UIKit/UINavigationBar.h, this class allows you to add one or two buttons to the kind of blue iPhone-looking bar shown here. Use these buttons to move between program screens or to add direct functionality to your application.
Here is the code for a very simple application that makes use of the Navigation Bar by placing two buttons on the top of the screen. Tap the buttons to update the displayed text. The source for this application includes:
To use a navigation bar, you must allocate it and initialize it with a frame. The frame defines the boundaries the bar will occupy in your window. The standard frame for one- or two-button navigation bars is 320 pixels wide by 48 pixels high frame based at the top-left of the iPhone window.
nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)]; The showButtonsWithLeftTitle: rightTitle: leftBack: method sets the number and kinds of buttons. Set leftBack: to YES or NO to specify whether the left button uses the back-pointing shape you see for many iPhone applications. Pass standard NSStrings to each title or use nil to omit the button from the display. e.g.
e.g. [nav showButtonsWithLeftTitle: @"Hello" rightTitle: @"Bye" leftBack: NO]; produces the two buttons shown here.
[nav showButtonsWithLeftTitle: @"Back" rightTitle: nil leftBack: YES]; shows a single back-styled Back button.
You can set the names on your buttons at any time as well as update their appearance. For example, you can jump between the two presentations examples I just listed above without penality.
Make sure to set the navigation bar's delegate to your main application to be able to receive the navigationBar: buttonClicked: message. The button number (0 or 1) tells you which item was tapped. In this sample, the text updates (goodbye or hello) based on the clicked button.


![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
Will said 1:24PM on 8-16-2007
Thank you! I've been working on a couple of apps, but Objective C and Cocoa are strange and new to me. Please keep these kind of posts coming.
Reply
Patrick McCarron said 2:44PM on 8-16-2007
What Will said! I got a game in progress, and I spend more time on figuring the little things out in the little time I have. These posts are very educational.
Reply
jtbandes said 3:21PM on 8-16-2007
Awesome... Have you figured out how to make the nice transitions with the "back"-style buttons?
As a side note, is there a -showButonsWithLeftTitle:rightTitle:rightForward: method?
Reply
manny said 3:38PM on 8-16-2007
Wow, great article, this really helped me
Reply
Rick Fillion said 4:52PM on 8-16-2007
*$%^&! I want an iPhone now more than ever. COME ON ROGERS, ANNOUNCE THE DAMN THING!
Reply
michas_pi said 7:14PM on 8-16-2007
In before "Enough of the iPhone news, TUAW!!!!!1111"
Reply
John said 2:17AM on 8-17-2007
I have a short piece on my blog explaining how to use the regular UIPushButton class, and its derivatives, too for those playing with coding apps for the iPhone.
Reply