An object that manages a view hierarchy for your UIKit app.


@interface UIViewController : UIResponder


The UIViewController class defines the shared behavior that is common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

A view controller’s main responsibilities include the following:

  • Updating the contents of the views, usually in response to changes to the underlying data.

  • Responding to user interactions with views.

  • Resizing views and managing the layout of the overall interface.

  • Coordinating with other objects—including other view controllers—in your app.

A view controller is tightly bound to the views it manages and takes part in handling events in its view hierarchy. Specifically, view controllers are UIResponder objects and are inserted into the responder chain between the view controller’s root view and that view’s superview, which typically belongs to a different view controller. If none of the view controller’s views handle an event, the view controller has the option of handling the event or passing it along to the superview.

View controllers are rarely used in isolation. Instead, you often use multiple view controllers, each of which owns a portion of your app’s user interface. For example, one view controller might display a table of items while a different view controller displays the selected item from that table. Usually, only the views from one view controller are visible at a time. A view controller may present a different view controller to display a new set of views, or it may act as a container for other view controllers’ content and animate views however it wants.

Subclassing Notes

Every app contains at least one custom subclass of UIViewController. More often, apps contain many custom view controllers. Custom view controllers define the overall behaviors of your app, including the app’s appearance and how it responds to user interactions. The following sections provide a brief overview of some of the tasks your custom subclass performs. For detailed information about using and implementing view controllers, see View Controller Programming Guide for iOS.

View Management

Each view controller manages a view hierarchy, the root view of which is stored in the view property of this class. The root view acts primarily as a container for the rest of the view hierarchy. The size and position of the root view is determined by the object that owns it, which is either a parent view controller or the app’s window. The view controller that is owned by the window is the app’s root view controller and its view is sized to fill the window.

View controllers load their views lazily. Accessing the view property for the first time loads or creates the view controller’s views. There are several ways to specify the views for a view controller:

  • Specify the view controller and its views in your app’s Storyboard. Storyboards are the preferred way to specify your views. With a storyboard, you specify the views and their connections to the view controller. You also specify the relationships and segues between your view controllers, which makes it easier to see and modify your app'€™s behavior.

    To load a view controller from a storyboard, call the instantiateViewControllerWithIdentifier: method of the appropriate UIStoryboard object. The storyboard object creates the view controller and returns it to your code.

  • Specify the views for a view controller using a Nib file. A nib file lets you specify the views of a single view controller but does not let you define segues or relationships between view controllers. The nib file also stores only minimal information about the view controller itself.

    To initialize a view controller object using a nib file, create your view controller class programmatically and initialize it using the initWithNibName:bundle: method. When its views are requested, the view controller loads them from the nib file.

  • Specify the views for a view controller using the loadView method. In that method, create your view hierarchy programmatically and assign the root view of that hierarchy to the view controller’s view property.

All of these techniques have the same end result, which is to create the appropriate set of views and expose them through the view property.

A view controller’s root view is always sized to fit its assigned space. For other views in your view hierarchy, use Interface Builder to specify the Auto Layout constraints that govern how each view is positioned and sized within its superview’s bounds. You can also create constraints programmatically and add them to your views at appropriate times. For more information about how to create constraints, see Auto Layout Guide.

Handling View-Related Notifications

When the visibility of its views changes, a view controller automatically calls its own methods so that subclasses can respond to the change. Use a method like viewWillAppear: to prepare your views to appear onscreen, and use the viewWillDisappear: to save changes or other state information. Use other methods to make appropriate changes.

Figure 1 shows the possible visible states for a view controller’s views and the state transitions that can occur. Not all ‘will’ callback methods are paired with only a ‘did’ callback method. You need to ensure that if you start a process in a ‘will’ callback method, you end the process in both the corresponding ‘did’ and the opposite ‘will’ callback method.

Figure 1

Valid State Transitions

Handling View Rotations

As of iOS 8, all rotation-related methods are deprecated. Instead, rotations are treated as a change in the size of the view controller’s view and are therefore reported using the viewWillTransitionToSize:withTransitionCoordinator: method. When the interface orientation changes, UIKit calls this method on the window’s root view controller. That view controller then notifies its child view controllers, propagating the message throughout the view controller hierarchy.

In iOS 6 and iOS 7, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported. The intersection of the app'€™s orientation mask and the view controller'€™s orientation mask is used to determine which orientations a view controller can be rotated into.

You can override the preferredInterfaceOrientationForPresentation for a view controller that is intended to be presented full screen in a specific orientation.

When a rotation occurs for a visible view controller, the willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are called during the rotation. The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent. If a view controller is not visible when an orientation change occurs, then the rotation methods are never called. However, the viewWillLayoutSubviews method is called when the view becomes visible. Your implementation of this method can call the statusBarOrientation method to determine the device orientation.

Implementing a Container View Controller

A custom UIViewController subclass can also act as a container view controller. A container view controller manages the presentation of content of other view controllers it owns, also known as its child view controllers. A child'€™s view can be presented as-is or in conjunction with views owned by the container view controller.

Your container view controller subclass should declare a public interface to associate its children. The nature of these methods is up to you and depends on the semantics of the container you are creating. You need to decide how many children can be displayed by your view controller at once, when those children are displayed, and where they appear in your view controller'€™s view hierarchy. Your view controller class defines what relationships, if any, are shared by the children. By establishing a clean public interface for your container, you ensure that children use its capabilities logically, without accessing too many private details about how your container implements the behavior.

Your container view controller must associate a child view controller with itself before adding the child'€™s root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child'€™s root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container'€™s implementation to provide the expected containment behavior.

Here are the essential methods you might need to call:

Memory Management

Memory is a critical resource in iOS, and view controllers provide built-in support for reducing their memory footprint at critical times. The UIViewController class provides some automatic handling of low-memory conditions through its didReceiveMemoryWarning method, which releases unneeded memory.

State Preservation and Restoration

If you assign a value to the view controller'€™s restorationIdentifier property, the system may ask the view controller to encode itself when the app transitions to the background. When preserved, a view controller preserves the state of any views in its view hierarchy that also have restoration identifiers. View controllers do not automatically save any other state. If you are implementing a custom container view controller, you must encode any child view controllers yourself. Each child you encode must have a unique restoration identifier.

For more information about how the system determines which view controllers to preserve and restore, see App Programming Guide for iOS. To see an example of state preservation and restoration, see Restoring Your App’s State.


Creating a View Controller

- initWithNibName:bundle:

Returns a newly initialized view controller with the nib file in the specified bundle.

Getting the Storyboard and Nib Information


The storyboard from which the view controller originated.


The name of the view controller'€™s nib file, if one was specified.


The view controller'€™s nib bundle if it exists.

Managing the View


The view that the controller manages.


The view controller’s view, or nil if the view is not yet loaded.


A Boolean value indicating whether the view is currently loaded into memory.

- loadView

Creates the view that the controller manages.

- viewDidLoad

Called after the controller'€™s view is loaded into memory.

- loadViewIfNeeded

Loads the view controller’s view if it has not yet been loaded.


A localized string that represents the view this controller manages.


The preferred size for the view controller’s view.

Responding to View-Related Events

- viewWillAppear:

Notifies the view controller that its view is about to be added to a view hierarchy.

- viewDidAppear:

Notifies the view controller that its view was added to a view hierarchy.

- viewWillDisappear:

Notifies the view controller that its view is about to be removed from a view hierarchy.

- viewDidDisappear:

Notifies the view controller that its view was removed from a view hierarchy.


A Boolean value indicating whether the view controller is being dismissed.


A Boolean value indicating whether the view controller is being presented.


A Boolean value indicating whether the view controller is being removed from a parent view controller.


A Boolean value indicating whether the view controller is being moved to a parent view controller.

Extending the View's Safe Area

Positioning Content Relative to the Safe Area

Position views so that they are not obstructed by other content.


Custom insets that you specify to modify the view controller's safe area.

- viewSafeAreaInsetsDidChange

Called to notify the view controller that the safe area insets of its root view changed.

Managing the View's Margins

Positioning Content Within Layout Margins

Position views so that they are not crowded by other content.


A Boolean value indicating whether the view controller's view uses the system-defined minimum layout margins.


The minimum layout margins for the view controller's root view.

- viewLayoutMarginsDidChange

Called to notify the view controller that the layout margins of its root view changed.

Configuring the View’s Layout Behavior


The edges that you extend for your view controller.


Constants that specify the edges of a rectangle.


A Boolean value indicating whether or not the extended layout includes opaque bars.

- viewWillLayoutSubviews

Called to notify the view controller that its view is about to layout its subviews.

- viewDidLayoutSubviews

Called to notify the view controller that its view has just laid out its subviews.

- updateViewConstraints

Called when the view controller'€™s view needs to update its constraints.

Configuring the View Rotation Settings


A Boolean value that indicates whether the view controller'€™s contents should autorotate.


The interface orientations that the view controller supports.


The interface orientation to use when presenting the view controller.

+ attemptRotationToDeviceOrientation

Attempts to rotate all windows to the orientation of the device.

Performing Segues

- shouldPerformSegueWithIdentifier:sender:

Determines whether the segue with the specified identifier should be performed.

- prepareForSegue:sender:

Notifies the view controller that a segue is about to be performed.

- performSegueWithIdentifier:sender:

Initiates the segue with the specified identifier from the current view controller'€™s storyboard file.

- allowedChildViewControllersForUnwindingFromSource:

Returns an array of child view controllers to search for an unwind segue destination.

- childViewControllerContainingSegueSource:

Returns the child view controller that contains the source of the unwind segue.

- canPerformUnwindSegueAction:fromViewController:sender:

Called on a view controller to determine whether it responds to an unwind action.

- unwindForSegue:towardsViewController:

Called when an unwind segue transitions to a new view controller.

Presenting a View Controller

- showViewController:sender:

Presents a view controller in a primary context.

- showDetailViewController:sender:

Presents a view controller in a secondary (or detail)€ context.

- presentViewController:animated:completion:

Presents a view controller modally.

- dismissViewControllerAnimated:completion:

Dismisses the view controller that was presented modally by the view controller.


The presentation style for modally presented view controllers.


Modal presentation styles available when presenting view controllers.


The transition style to use when presenting the view controller.


Transition styles available when presenting view controllers.


A Boolean value indicating whether the view controller enforces a modal behavior.


A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.


A Boolean value that indicates whether the view controller specifies the transition style for view controllers it presents.


Returns a Boolean indicating whether the current input view is dismissed automatically when changing controls.


Posted when a split view controller is expanded or collapsed.

Adding a Custom Transition or Presentation


The delegate object that provides transition animator, interactive controller, and custom presentation controller objects.


Returns the active transition coordinator object.

- targetViewControllerForAction:sender:

Returns the view controller that responds to the action.


The presentation controller that’s managing the current view controller.


The nearest popover presentation controller that is managing the current view controller.


A Boolean value that indicates whether an item that previously was focused should again become focused when the item's view controller becomes visible and focusable.

Adapting to Environment Changes

- collapseSecondaryViewController:forSplitViewController:

Called when a split view controller transitions to a compact-width size class.

- separateSecondaryViewControllerForSplitViewController:

Called when a split view controller transitions to a regular-width size class.

Adjusting the Interface Style


The user interface style adopted by the view controller and all of its children.


The preferred interface style for this view controller.


The child view controller that supports the preferred user interface style.

- setNeedsUserInterfaceAppearanceUpdate

Notifies the view controller that a change occurred that might affect the preferred interface style.


Constants indicating the interface style for the app.

Managing Child View Controllers in a Custom Container


An array of view controllers that are children of the current view controller.

- addChildViewController:

Adds the specified view controller as a child of the current view controller.

- removeFromParentViewController

Removes the view controller from its parent.

- transitionFromViewController:toViewController:duration:options:animations:completion:

Transitions between two of the view controller'€™s child view controllers.


Returns a Boolean value indicating whether appearance methods are forwarded to child view controllers.

- beginAppearanceTransition:animated:

Tells a child controller its appearance is about to change.

- endAppearanceTransition

Tells a child controller its appearance has changed.

- setOverrideTraitCollection:forChildViewController:

Changes the traits assigned to the specified child view controller.

- overrideTraitCollectionForChildViewController:

Retrieves the trait collection for a child view controller.


Raised if the view controller hierarchy is inconsistent with the view hierarchy.

Responding to Containment Events

- willMoveToParentViewController:

Called just before the view controller is added or removed from a container view controller.

- didMoveToParentViewController:

Called after the view controller is added or removed from a container view controller.

Getting Other Related View Controllers


The view controller that presented this view controller.


The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.


The parent view controller of the recipient.


The nearest ancestor in the view controller hierarchy that is a split view controller.


The nearest ancestor in the view controller hierarchy that is a navigation controller.


The nearest ancestor in the view controller hierarchy that is a tab bar controller.

Configuring a Navigation Interface


The navigation item used to represent the view controller in a parent'€™s navigation bar.


A Boolean value indicating whether the toolbar at the bottom of the screen is hidden when the view controller is pushed on to a navigation controller.

- setToolbarItems:animated:

Sets the toolbar items to be displayed along with the view controller.


The toolbar items associated with the view controller.

Configuring Tab Bar Content


The tab bar item that represents the view controller when added to a tab bar controller.


The full-screen scroll view to synchronize with a scrolling tab bar.

Supporting App Extensions


Returns the extension context of the view controller.

Coordinating with System Gestures


The screen edges for which you want your gestures to take precedence over the system gestures


Returns the child view controller that should be queried to see if its gestures should take precedence.

- setNeedsUpdateOfScreenEdgesDeferringSystemGestures

Call this method when you change the screen edges that you use for deferring system gestures.


Returns a Boolean indicating whether the system is allowed to hide the visual indicator for returning to the Home screen.


Returns the child view controller that is consulted about its preference for displaying a visual indicator for returning to the Home screen.

- setNeedsUpdateOfHomeIndicatorAutoHidden

Notifies UIKit that your view controller updated its preference regarding the visual indicator for returning to the Home screen.

Managing the Status Bar


Called when the system needs the view controller to use for determining status bar hidden/unhidden state.


Called when the system needs the view controller to use for determining status bar style.


The preferred status bar style for the view controller.


Specifies whether the view controller prefers the status bar to be hidden or shown.


Specifies whether a view controller, presented non-fullscreen, takes over control of status bar appearance from the presenting view controller.


Specifies the animation style to use for hiding and showing the status bar for the view controller.

- setNeedsStatusBarAppearanceUpdate

Indicates to the system that the view controller status bar attributes have changed.

Accessing the Available Key Commands


A Boolean value indicating whether the view controller performs menu-related actions.

- addKeyCommand:

Associates the specified keyboard shortcut with the view controller.

- removeKeyCommand:

Removes the key command from the view controller.

Adding Editing Behaviors to Your View Controller


A Boolean value indicating whether the view controller currently allows the user to edit the view contents.

- setEditing:animated:

Sets whether the view controller shows an editable view.


Returns a bar button item that toggles its title and associated state between Edit and Done.

Handling Memory Warnings

- didReceiveMemoryWarning

Sent to the view controller when the app receives a memory warning.

Managing State Restoration

Restoring Your App’s State

Preserve and restore information related to the user’s current activities.


The identifier that determines whether the view controller supports state restoration.


The class responsible for recreating this view controller when restoring the app'€™s state.

- encodeRestorableStateWithCoder:

Encodes state-related information for the view controller.

- decodeRestorableStateWithCoder:

Decodes and restores state-related information for the view controller.

- applicationFinishedRestoringState

Called on restored view controllers after other object decoding is complete.

See Also

Content View Controllers

Displaying and Managing Views with a View Controller

Build a view controller in storyboards, configure it with custom views, and fill those views with your app’s data.

Showing and Hiding View Controllers

Display view controllers using different techniques, and pass data between them during transitions.


A view controller that specializes in managing a table view.


A view controller that specializes in managing a collection view.


A set of methods for adapting the contents of your view controllers to size and trait changes.