UINavigationController

UINavigationController is a view conntroller that implements a stack-based navigating.

UINavigationBar object that displays navigation controls.

UINavigationItem represents item that a navigation bar displays when the associated view controller is visible: left or back button, right button, title or view for center of navigation bar.

A navigation controller object 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.

UIViewController class has some properties related with navigation controller.

  • navigationController - refers to the owner navigation controller
  • navigationItem - represents the view controller in a parent's navigation bar
  • title - can be used as title in tool bar

main properties

There are some usefull properties of UINavigationController.

The viewControllers property is an ordered array, that contains the view controllers currently on the navigation stack. The root view controller is at index 0 and the top controller is at index n-1, where n is the number of items in the array.

The navigationBar property refers to the UINavigationBar from which you can access to an array of navigation items managed by the navigation bar (items property). Each item is provided by child view controller (navigationItem property).

The delegate property refers to the UINavigationControllerDelegate object. It can override the pushing or popping of view controllers, provide custom animation transitions, and specify the preferred orientation for the navigation interface.

Additionally you can use following properties:

  • topViewController - the view controller at the top of the navigation stack.
  • visibleViewController - the view controller associated with the currently visible view in the navigation interface.
  • interactivePopGestureRecognizer - the gesture recognizer responsible for popping the top view controller off the navigation stack

create navigation

Create new cocoa touch file NavigationController as subclass of UINavigationController class.

To add a navigation controller to your interface, drag a UINavigationController to your storyboard. Xcode creates a new scene that includes a navigation controller and table view controller as root controller. Now specify our subclass of UINavigationController.

If necessary specify navigation controller as initial controller.

Suppose we don't want default root controller. Let's break a link between a navigation controller and a table controller. Then we can delete table controller.

Now let's make two view controllers and set first view controller (ViewController1) as root controller of navigation.

And the last step is to add a button to the first view controller and link it to the second view controller. As a result, when the user clicks the button, the second controller will be shown.

Press the control key and drag this button onto the second controller. Then select show from the context menu.

Finally run app.

Note. There is other way to add navigation controller. First add root view controller, then embed it into a navigation controller:

select view controller then 
   menu -> Editor -> Embed In -> Navigation Controller

There are some methods to control navigation.

// show controller from storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "ViewController2")
self.navigationController?.pushViewController(vc, animated: true)

// back to the previous controller (same as pressing back button)
self.navigationController?.popViewController(animated: true)

// back to the root controller
navigationController?.popToRootViewController(animated: true)

It is possible remove controllers directly via viewControllers property.

var vcarray = self.navigationController?.viewControllers
vcarray?.removeLast() // remove the last controller
vcarray?.removeFirst() // remove the first controller
vcarray?.removeAll() // remove all controllers
vcarray?.remove(at: n) // remove controller at specific position

back swipe

A navigation controller supports a back swipe gesture to close top controller.

A back swipe works when navigation bar is visible and has a default back button.

to enable a back swipe with hidden navigation bar you need:

  • enablet a pop gesture recognizer
  • set a pop gesture recognizer gelegate to nil
Enable a back swipe with the hidden navigation bar