UINavigationController
has 3 controllers that all sound very similar to me: topViewController
, visibleViewController
, and presentedViewController
.
Which scenarios do you use each of these controllers in?
@MainActor class UINavigationController : UIViewController A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.
The navigation controller pushes the specified view controller onto its navigation stack. The navigation controller presents the specified view controller modally. The behaviors of other segue types are unchanged. A navigation controller supports the following behaviors for its interface:
When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.
Tapping the back button in the navigation bar at the top of the interface removes the top view controller, thereby revealing the view controller underneath. Use a navigation interface to mimic the organization of hierarchical data managed by your app.
pushViewController(_:animated:)
method. Pushes the previous controller out of it's way and replaces it. 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. Example:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With