iPhone OS Reference Library Apple Developer Connection spyglass button

UINavigationController Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iPhone OS 2.0 and later.
Companion guide
Declared in
UINavigationController.h
Related sample code

Overview

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This class is not intended for subclassing. Instead, you use instances of it as-is in situations where you want your application’s user interface to reflect the hierarchical nature of your content. This navigation interface makes it possible to present your data efficiently and also makes it easier for the user to navigate that content.

The screens presented by a navigation interface typically mimic the hierarchical organization of your data. At each level of the hierarchy, you provide an appropriate screen (managed by a custom view controller) to display the content at that level. Figure 1 shows an example of the navigation interface presented by the Settings application in iPhone Simulator. The first screen presents the user with the list of applications that contain preferences. Selecting an application reveals individual settings and groups of settings for that application. Selecting a group yields more settings and so on. For all but the root view, the navigation controller provides a back button to allow the user to move back up the hierarchy.

Figure 1  A sample navigation interface

A sample navigation interface

A navigation controller object manages the currently displayed screens using the navigation stack. At the bottom of this stack is the root view controller and at the top of the stack is the view controller currently being displayed. You use the methods of your navigation controller object to modify the stack at runtime. The most common operation is to push new view controllers onto the stack using the pushViewController:animated: method. Pushing a new view controller object onto the stack causes the view of that view controller to be displayed and the navigation controls to be updated to reflect the change. You typically push view controllers in response to the user selecting an item that leads to the next level in your information hierarchy.

In addition to pushing view controllers onto the navigation stack, you can also pop them using the popViewControllerAnimated: method. Although you can pop view controllers yourself, the navigation controller also provides a back button (when appropriate) that pops the top view controller in response to user interactions.

A navigation controller object notifies its delegate object in response to changes in the active view controller. The delegate object is a custom object provided by your application that conforms to the UINavigationControllerDelegate protocol. You can use the methods of this protocol to respond to the change and perform additional setup or cleanup tasks.

For more information about how to integrate navigation controllers into your application, see View Controller Programming Guide for iPhone OS.

Navigation Controller Views

Because the UINavigationController class inherits from the UIViewController class, navigation controllers have their own view that is accessible through the view property. When deploying a navigation interface, you must install this view as the root of whatever view hierarchy you are creating. For example, if you are deploying the navigation interface by itself, you would make this view the main subview of your window. To install a navigation interface inside a tab bar interface, you would install the navigation controller’s view as the root view of the appropriate tab.

The view for a navigation controller is just a container for several other views, including a navigation bar, an optional toolbar, and the view containing your custom content. Figure 2 shows how these views are assembled to present the overall navigation interface. Although the content of the navigation bar and toolbar views changes, the views themselves do not. Only the custom content view changes to reflect the view controller that is at the top of the navigation stack.

Figure 2  The views of a navigation controller

The views of a navigation controller
Note: Because the amount of space available for the custom view can vary (depending on the size of the other navigation views), your custom view’s autoresizingMask property should be set to have a flexible width and height. Before displaying your view, the navigation controller automatically positions and sizes it to fit the available space.

The navigation controller is responsible for managing the configuration and display of the navigation bar and navigation toolbar. You must never modify these views directly. Instead, you should manipulate them through the methods and properties of the UINavigationController class. You can hide and show the navigation bar using the navigationBarHidden property or setNavigationBarHidden:animated: method. To specify custom items for the navigation bar, you configure the displayed view controller as described in “Updating the Navigation Bar.” For information on how to specify items for the navigation toolbar, see “Displaying a Toolbar.”

With only a few exceptions, you should never modify the navigation bar object directly. It is permissible to modify the barStyle or translucent properties of the navigation bar but you must never change its frame, bounds, or alpha values directly. In addition, the navigation controller object builds the contents of the navigation bar dynamically using the navigation items (instances of the UINavigationItem class) associated with the view controllers on the navigation stack. To change the contents of the navigation bar, you must therefore configure the navigation items for your custom view controllers. For more information about navigation items, see UINavigationItem Class Reference.

Updating the Navigation Bar

When the user changes the top-level view controller, whether by pushing or popping a view controller or changing the contents of the navigation stack directly, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class. You can create items with custom content or create standard system items depending on your needs. For more information about how to create bar button items, see UIBarButtonItem Class Reference.

The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:

The navigation controller updates the middle of the navigation bar as follows:

The navigation controller updates the right side of the navigation bar as follows:

The navigation controller updates the navigation bar each time the top view controller changes. Thus, these changes occur each time a view controller is pushed onto the stack or popped from it. When you animate a push or pop operation, the navigation controller similarly animates the change in navigation bar content.

Displaying a Toolbar

In iPhone OS 3.0 and later, navigation controller objects make it easy to provide a custom toolbar for each screen of a navigation interface. The navigation controller object now manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller. When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.

The navigation toolbar is hidden by default but you can show it for your navigation interface by calling the setToolbarHidden:animated: method of your navigation controller object. If not all of your view controllers support toolbar items, your delegate object can call this method to toggle the visibility of the toolbar during subsequent push and pop operations.

Tasks

Creating Navigation Controllers

Accessing Items on the Navigation Stack

Pushing and Popping Stack Items

Configuring Navigation Bars

Accessing the Delegate

Configuring Custom Toolbars

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

delegate

The receiver’s delegate or nil if it doesn’t have a delegate.

@property(nonatomic, assign) id<UINavigationControllerDelegate> delegate
Discussion

See UINavigationControllerDelegate Protocol Reference for the methods this delegate should implement.

Availability
Declared In
UINavigationController.h

navigationBar

The navigation bar managed by the navigation controller. (read-only)

@property(nonatomic, readonly) UINavigationBar *navigationBar
Discussion

It is permissible to modify the barStyle or translucent properties of the navigation bar but you must never change its frame, bounds, or alpha values directly. To show or hide the navigation bar, you should always do so through the navigation controller by changing its navigationBarHidden property or calling the setNavigationBarHidden:animated: method.

Availability
Related Sample Code
Declared In
UINavigationController.h

navigationBarHidden

A Boolean value that determines whether the navigation bar is hidden.

@property(nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden
Discussion

If YES, the navigation bar is hidden. The default value is NO. Setting this property does not animate the hiding or showing of the navigation bar; use setNavigationBarHidden:animated: for that purpose.

Availability
Declared In
UINavigationController.h

toolbar

The custom toolbar associated with the navigation controller. (read-only)

@property(nonatomic,readonly) UIToolbar *toolbar
Discussion

This property contains a reference to the built-in toolbar managed by the navigation controller. Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly.

Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.

The visibility of this toolbar is controlled by the toolbarHidden property. The toolbar also obeys the hidesBottomBarWhenPushed property of the currently visible view controller and hides and shows itself automatically as needed.

Availability
See Also
Declared In
UINavigationController.h

toolbarHidden

A Boolean indicating whether the navigation controller’s built-in toolbar is visible.

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden
Discussion

If this property is set to YES, the toolbar is not visible. The default value of this property is YES.

Availability
See Also
Declared In
UINavigationController.h

topViewController

The view controller at the top of the navigation stack. (read-only)

@property(nonatomic, readonly, retain) UIViewController *topViewController
Availability
See Also
Related Sample Code
Declared In
UINavigationController.h

viewControllers

The view controllers currently on the navigation stack.

@property(nonatomic, copy) NSArray *viewControllers
Discussion

The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to NO.

Availability
See Also
Declared In
UINavigationController.h

visibleViewController

The view controller associated with the currently visible view in the navigation interface. (read-only)

@property(nonatomic, readonly, retain) UIViewController *visibleViewController
Discussion

The currently visible view can belong either to the view controller at the top of the navigation stack or to a view controller that was presented modally.

Availability
See Also
Declared In
UINavigationController.h

Instance Methods

initWithRootViewController:

Initializes and returns a newly created navigation controller.

- (id)initWithRootViewController:(UIViewController *)rootViewController

Parameters
rootViewController

The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.

Return Value

The initialized navigation controller object or nil if there was a problem initializing the object.

Discussion

This is a convenience method for initializing the receiver and pushing a root view controller onto the navigation stack. Every navigation stack must have at least one view controller to act as the root.

Availability
  • Available in iPhone OS 2.0 and later.
Related Sample Code
Declared In
UINavigationController.h

popToRootViewControllerAnimated:

Pops all the view controllers on the stack except the root view controller and updates the display.

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

Parameters
animated

Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value

An array of view controllers that are popped from the stack.

Discussion

The root view controller becomes the top view controller. For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UINavigationController.h

popToViewController:animated:

Pops view controllers until the specified view controller is at the top of the navigation stack.

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

Parameters
viewController

The view controller that you want to be at the top of the stack.

animated

Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value

An array containing the view controllers that were popped from the stack.

Discussion

For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UINavigationController.h

popViewControllerAnimated:

Pops the top view controller from the navigation stack and updates the display.

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

Parameters
animated

Set this value to YES to animate the transition. Pass NO if you are setting up a navigation controller before its view is displayed.

Return Value

The view controller that was popped from the stack.

Discussion

This method removes the top view controller from the stack and makes the new top of the stack the active view controller. If the view controller at the top of the stack is the root view controller, this method does nothing. In other words, you cannot pop the last item on the stack.

In addition to displaying the view associated with the new view controller at the top of the stack, this method also updates the navigation bar and tool bar accordingly. In iPhone OS 3.0 and later, the contents of the built-in navigation toolbar are updated to reflect the toolbar items of the new view controller. For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UINavigationController.h

pushViewController:animated:

Pushes a view controller onto the receiver’s stack and updates the display.

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

Parameters
viewController

The view controller that is pushed onto the stack. This object cannot be an instance of tab bar controller and it must not already be on the navigation stack.

animated

Specify YES to animate the transition or NO if you do not want the transition to be animated. You might specify NO if you are setting up the navigation controller at launch time.

Discussion

The object in the viewController parameter becomes the top view controller on the navigation stack. Pushing a view controller results in the display of the view it manages. How that view is displayed is determined by the animated parameter. If the animated parameter is YES, the view is animated into position; otherwise, the view is simply displayed in place. The view is automatically resized to fit between the navigation bar and toolbar (if present) before it is displayed.

In addition to displaying the view associated with the new view controller at the top of the stack, this method also updates the navigation bar and tool bar accordingly. In iPhone OS 3.0 and later, the contents of the built-in navigation toolbar are updated to reflect the toolbar items of the new view controller. For information on how the navigation bar is updated, see “Updating the Navigation Bar.”

Important: In iPhone OS 2.2 and later, if the object in the viewController parameter is already on the navigation stack, this method throws an exception. In earlier versions of iPhone OS, the method simply does nothing.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UINavigationController.h

setNavigationBarHidden:animated:

Sets whether the navigation bar is hidden.

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden

Specify YES to hide the navigation bar or NO to show it.

animated

Specify YES if you want to animate the change in visibility or NO if you want the navigation bar to appear immediately.

Discussion

For animated transitions, the duration of the animation is specified by the value in the UINavigationControllerHideShowBarDuration constant.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UINavigationController.h

setToolbarHidden:animated:

Changes the visibility of the navigation controller’s built-in toolbar.

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated

Parameters
hidden

Specify YES to hide the toolbar or NO to show it.

animated

Specify YES if you want the toolbar to be animated on or off the screen.

Discussion

You can use this method to animate changes to the visibility of the built-in toolbar.

Calling this method with the animated parameter set to NO is equivalent to setting the value of the toolbarHidden property directly. The toolbar simply appears or disappears depending on the value in the hidden parameter.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Declared In
UINavigationController.h

setViewControllers:animated:

Replaces the view controllers currently managed by the navigation controller with the specified items.

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

Parameters
viewControllers

The view controllers to place in the stack. The front-to-back order of the controllers in this array represents the new bottom-to-top order of the controllers in the navigation stack. Thus, the last item added to the array becomes the top item of the navigation stack.

animated

If YES, animate the pushing or popping of the top view controller. If NO, replace the view controllers without any animations.

Discussion

You can use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

If animations are enabled, this method decides which type of transition to perform based on whether the last item in the items array is already in the navigation stack. If the view controller is currently in the stack, but is not the topmost item, this method uses a pop transition; if it is the topmost item, no transition is performed. If the view controller is not on the stack, this method uses a push transition. Only one transition is performed, but when that transition finishes, the entire contents of the stack are replaced with the new view controllers. For example, if controllers A, B, and C are on the stack and you set controllers D, A, and B, this method uses a pop transition and the resulting stack contains the controllers D, A, and B.

Availability
  • Available in iPhone OS 3.0 and later.
Declared In
UINavigationController.h

Constants

UINavigationControllerHideShowBarDuration

A global variable that specifies a preferred duration when animating the navigation bar.

extern const CGFloat UINavigationControllerHideShowBarDuration
Constants
UINavigationControllerHideShowBarDuration

This variable specifies the duration when animating the navigation bar. Note that this is a constant value, so it cannot be set.

Available in iPhone OS 2.0 and later.

Declared in UINavigationController.h.



Last updated: 2009-05-22

Did this document help you? Yes It's good, but... Not helpful...