In UINavigationController what is the difference between topViewController, visibleViewController, presentedViewController? In UINavigationController what is the difference between topViewController, visibleViewController, presentedViewController? ios ios

In UINavigationController what is the difference between topViewController, visibleViewController, presentedViewController?


  • topViewController - last view controller pushed onto the UINavigationController using UINavigationController's pushViewController(_:animated:) method. Pushes the previous controller out of it's way and replaces it.
  • presentedViewController - view controller that is presented over top of another (basically it covers up another view controller instead of pushing it out of the way). INSTEAD of UINavigationController's pushViewController(_:animated:) you use UIViewController's present(_:animated:completion:) method. Note: Presented view controllers are also referred to as modal view controllers and can be used WITHOUT a UINavigationController.
  • visibleViewController could be the same as topViewController OR presentedViewController. It would be the same as topViewController if you pushed onto the UINavigationController last. It would be the same as the presentedViewController if you presented on a UIViewController last.

Example:

  1. Push UIViewController viewA onto a UINavigationController.
  2. Have viewA present UIViewController viewB over itself.
  3. viewA is topViewController.
  4. viewB is presentedViewController.
  5. viewB is also visibleViewController.
  6. Dismiss modal viewB.
  7. viewA is now topViewController AND visibleViewController. (There is no presentedViewController.)
  8. Pop viewA.
  9. viewA is no longer visibleViewController OR topViewController.

In general it seems like visibleViewController is more useful since it will tell you what view is currently showing regardless of if it was pushed or presented.


presentedViewController is the current modal presented on screen.topViewController is view controller on top of the navigation stack (see viewControllers() method) and visibleViewController is the currently displayed view controller on screen (can be either a controller, a modal, a UINavigationController, a UITabbarController, etc).


  • TopViewController is the topmost VC in the navigation stack. OR the View controller which was pushed last.

  • PresentedViewController is the viewController presented modally from the navigationController. It is not part of NaivagationController's horizontal stack, but a viewController presented over it.

  • Of these two viewControllers, the one that is visible to the user will be the visibleViewController of the navigationController.


matomo