Class

UINavigationBar

The UINavigationBar class provides a control for navigating hierarchical content. It’s a bar, typically displayed at the top of the screen, containing buttons for navigating within a hierarchy of screens. The primary properties are a left (back) button, a center title, and an optional right button. You can use a navigation bar as a standalone object or in conjunction with a navigation controller object.

Overview

Using a Navigation Bar 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.

A navigation controller automatically assigns itself as the delegate of its navigation bar object. Therefore, when using a navigation controller, don’t assign a custom delegate object to the corresponding navigation bar.

For information about navigation controllers, see UINavigationController.

Customizing the Appearance of a Navigation Bar

In iOS 5.0 and later, you can customize the appearance of the bar using the methods listed in Customizing the Bar Appearance. You can customize the appearance of all navigation bars using the appearance proxy ([UINavigationBar appearance]), or just of a single bar.

In iOS 7, a navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images. The barTintColor property affects the color of the bar itself. Additionally, navigation bars are translucent by default. Turning the translucency off or on does not affect buttons, since they do not have backgrounds.

In general, when a property is dependent on the bar metrics (on the iPhone in landscape orientation, bars have a different height from standard), be sure to specify a value for default as well as for other metrics.

For more information about appearance and behavior configuration, see Navigation Bars.

Symbols

Assigning the Delegate

var delegate: UINavigationBarDelegate?

The navigation bar’s delegate object.

Pushing and Popping Items

func pushItem(UINavigationItem, animated: Bool)

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

func popItem(animated: Bool)

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

func setItems([UINavigationItem]?, animated: Bool)

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

var items: [UINavigationItem]?

An array of navigation items managed by the navigation bar.

var topItem: UINavigationItem?

The navigation item at the top of the navigation bar’s stack.

var backItem: UINavigationItem?

The navigation item that is immediately below the topmost item on navigation bar’s stack.

Customizing the Bar Appearance

var backIndicatorImage: UIImage?

The image shown beside the back button.

var backIndicatorTransitionMaskImage: UIImage?

The image used as a mask for content during push and pop transitions.

var barStyle: UIBarStyle

The navigation bar style that specifies its appearance.

var barTintColor: UIColor?

The tint color to apply to the navigation bar background.

var shadowImage: UIImage?

The shadow image to be used for the navigation bar.

var tintColor: UIColor!

The tint color to apply to the navigation items and bar button items.

var isTranslucent: Bool

A Boolean value indicating whether the navigation bar is translucent (true) or not (false).

func backgroundImage(for: UIBarMetrics)

Returns the background image for given bar metrics.

func setBackgroundImage(UIImage?, for: UIBarMetrics)

Sets the background image for given bar metrics.

func backgroundImage(for: UIBarPosition, barMetrics: UIBarMetrics)

Returns the background image to use for a given bar position and set of metrics.

func setBackgroundImage(UIImage?, for: UIBarPosition, barMetrics: UIBarMetrics)

Sets the background image to use for a given bar position and set of metrics.

func titleVerticalPositionAdjustment(for: UIBarMetrics)

Returns the title’s vertical position adjustment for given bar metrics.

func setTitleVerticalPositionAdjustment(CGFloat, for: UIBarMetrics)

Sets the title’s vertical position adjustment for given bar metrics.

var titleTextAttributes: [String : Any]?

Display attributes for the bar’s title text.