iPhone OS Reference Library Apple Developer Connection spyglass button

UINavigationBar Class Reference

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

Overview

The UINavigationBar class implements a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating up and down a hierarchy. The primary properties are a left (back) button, a center title, and an optional right button. You can specify custom views for each of these.

You can use a navigation bar as a standalone object or in conjunction with a navigation controller object. To use a navigation bar as a standalone object, you create it and add it to your view hierarchy like you would any other view. Specifically, you can create it in Interface Builder and load it with the rest of your views or you can create it programmatically using the standard alloc and initWithFrame: methods.

You can modify the appearance of the bar using the barStyle, tintColor, and translucent properties. These properties affect the visual appearance of the bar itself but they also affect the way buttons are displayed in the bar. For example, if you set the translucent property to YES, any buttons in the bar are also made partially opaque.

For information about using a navigation bar with a navigation controller object, see “Using With a Navigation Controller.”

Adding Content to a Navigation Bar

When you use a navigation bar as a standalone object, you are responsible for providing its contents. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string.

A navigation bar manages a stack of UINavigationItem objects. Although the stack is there mostly to support navigation controllers, you can use it as well to implement your own custom navigation interface. The topmost item in the stack represents the navigation item whose contents are currently displayed by the navigation bar. You push new navigation items onto the stack using the pushNavigationItem:animated: method and pop items off the stack using the popNavigationItemAnimated: method. Both of these changes can be animated for the benefit of the user.

In addition to pushing and popping items, you can also set the contents of the stack directly using either the items property or the setItems:animated: method. You might use these methods at launch time to restore your interface to its previous state or to push or pop more than one navigation item at a time.

If you are using a navigation bar as a standalone object, you should assign a custom delegate object to the delegate property and use that object to intercept messages coming from the navigation bar. Delegate objects must conform to the UINavigationBarDelegate protocol. The delegate notifications let you know when the contents of responsible for deciding when items are pushed or popped from the stack—for example, it should display the previous view when the user clicks the back button.

For more information about creating navigation items, see UINavigationItem Class Reference. For more information about implementing a delegate object, see UINavigationBarDelegate Protocol Reference.

Using With a Navigation Controller

The most common way to use a navigation bar is in conjunction with a UINavigationController object. If you use a navigation controller to manage the navigation between different screens of content, the navigation controller creates the navigation bar automatically and pushes and pops navigation items when appropriate. You do not have to create the navigation bar and you do not have to manage the pushing and popping of navigation items yourself.

When used in conjunction with a navigation controller, there are only a handful of direct customizations you can make to the navigation bar. Specifically, it is alright to modify the barStyle, tintColor, and translucent properties of this class, but you must never directly change UIView-level properties such as the frame, bounds, alpha, or hidden properties directly. In addition, you should let the navigation controller manage the stack of navigation items and not attempt to modify these items yourself.

A navigation controller automatically assigns itself as the delegate of its navigation bar object. Therefore, when using a navigation controller, you must not attempt to assign a custom delegate object to the corresponding navigation bar.

Tasks

Configuring Navigation Bars

Assigning the Delegate

Pushing and Popping Items

Properties

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

backItem

The navigation item that is immediately below the topmost item on navigation bar’s stack. (read-only)

@property(nonatomic, readonly, retain) UINavigationItem *backItem
Discussion

If the leftBarButtonItem property of the topmost navigation item is nil, the navigation bar displays a back button whose title is derived from the item in this property.

If there is only one item on the navigation bar’s stack, the value of this property is nil.

Availability
See Also
Declared In
UINavigationBar.h

barStyle

The appearance of the navigation bar.

@property(nonatomic, assign) UIBarStyle barStyle
Discussion

See UIBarStyle for possible values. The default value is UIBarStyleDefault.

It is permissible to set the value of this property when the navigation bar is being managed by a navigation controller object.

Availability
Related Sample Code
Declared In
UINavigationBar.h

delegate

The navigation bar’s delegate object.

@property(nonatomic, assign) id delegate
Discussion

The delegate should conform to the UINavigationBarDelegate protocol. The default value is nil.

If the navigation bar was created by a navigation controller and is being managed by that object, you must not change the value of this property. Navigation controllers act as the delegate for any navigation bars they create.

Availability
Declared In
UINavigationBar.h

items

An array of navigation items managed by the navigation bar.

@property(nonatomic, copy) NSArray *items
Discussion

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

Availability
See Also
Declared In
UINavigationBar.h

tintColor

The color used to tint the bar.

@property(nonatomic, retain) UIColor *tintColor
Discussion

The default value is nil.

It is permissible to set the value of this property when the navigation bar is being managed by a navigation controller object.

Availability
Declared In
UINavigationBar.h

topItem

The navigation item at the top of the navigation bar’s stack. (read-only)

@property(nonatomic, readonly, retain) UINavigationItem *topItem
Availability
See Also
Related Sample Code
Declared In
UINavigationBar.h

translucent

A Boolean value indicating whether the navigation bar is only partially opaque.

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent
Discussion

Always set to YES if the barStyle property contains the value UIBarStyleBlackTranslucent. When YES, the navigation bar is drawn with partial opacity, regardless of the bar style. The amount of opacity is fixed and cannot be changed.

It is permissible to set the value of this property when the navigation bar is being managed by a navigation controller object.

Availability
Declared In
UINavigationBar.h

Instance Methods

popNavigationItemAnimated:

Pops the top item from the receiver’s stack and updates the navigation bar.

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated

Parameters
animated

YES if the navigation bar should be animated; otherwise, NO.

Return Value

The top item that was popped.

Discussion

Popping a navigation item removes the top item from the stack and replaces it with the back item. The back item’s title is centered on the navigation bar and its other properties are displayed.

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

pushNavigationItem:animated:

Pushes the given navigation item onto the receiver’s stack and updates the navigation bar.

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated

Parameters
item

The navigation item to push on the stack.

animated

YES if the navigation bar should be animated; otherwise, NO.

Discussion

Pushing a navigation item displays the item’s title in the center on the navigation bar. The previous top navigation item (if it exists) is displayed as a back button on the left side of the navigation bar. If the new top item has a left custom view, it is displayed instead of the back button.

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

setItems:animated:

Replaces the navigation items currently managed by the navigation bar with the specified items.

- (void)setItems:(NSArray *)items animated:(BOOL)animated

Parameters
items

The UINavigationItem objects to place in the stack. The front-to-back order of the items in this array represents the new bottom-to-top order of the items 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 stack item. If NO, replace the stack items without any animations.

Discussion

You can use this method to update or replace the navigation items in the stack without pushing or popping each item explicitly. In addition, this method lets you update the stack without animating the changes, which might be appropriate at launch time when you want to restore the state of the navigation stack to some 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 on the current navigation stack. If the item is currently on 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 item 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 items. For example, if items A, B, and C are on the stack and you set items D, A, and B, this method uses a pop transition and the resulting stack contains the items D, A, and B.

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


Last updated: 2009-08-05

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