skip to content

Navigation & Presentation

Push and pop stacks, tab bars, modal presentation and custom transitions, plus coordinators that pull routing out of view controllers. Architecture questions start here: who picks the next screen.

on this pageshow

explore

questions

6

In UIKit, how do you push and pop screens on a UINavigationController, and where does the Back button's title come from?

level: juniorimportance: must knowfreq 82%

answer

  1. an ordered stack of view controllers
  2. push adds on top, pop removes the top
  3. the root can never be popped
  4. the back item is the screen underneath
  5. backButtonTitle on the previous navigationItem

basics

~10 s

A UINavigationController keeps a stack of view controllers: pushViewController(_:animated:) adds one on top, popViewController(animated:) removes the top one and popToRootViewController(animated:) returns to the first. The Back title comes from the previous screen's navigationItem.

solid answer

~40 s

A `UINavigationController` shows one view controller at a time from its `viewControllers` stack: the first is the root, the last is on screen. `pushViewController(_:animated:)` slides a new screen on top, and `popViewController(animated:)` removes the top one and returns it; on the root it does nothing, because the root can't be popped. `popToViewController(_:animated:)` and `popToRootViewController(animated:)` remove several screens at once, and `setViewControllers(_:animated:)` replaces the whole stack with a single transition. The Back button belongs to the screen underneath: the bar uses that screen's `navigationItem.backBarButtonItem`, then its `backButtonTitle`, then its `title`, then a generic "Back". So to make the dish screen's Back button read "Menu", you set it on the menu screen.

code

swift · 25 lines
swift
import UIKit

final class MenuViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Luigi's Kitchen"
        // Shown by the NEXT screen's back button, not by this screen.
        navigationItem.backButtonTitle = "Menu"
    }

    func showDish(named name: String) {
        let dish = UIViewController()
        dish.title = name
        navigationController?.pushViewController(dish, animated: true)
    }

    func closeDish() {
        navigationController?.popViewController(animated: true)
    }

    func orderPlaced() {
        // Back to the restaurant list at the bottom of the stack.
        navigationController?.popToRootViewController(animated: true)
    }
}

go deeper

for a junior

Recall the four calls: push, pop, pop to a view controller, pop to root. Know that the root can't be popped and that the Back title is set on the screen underneath.

for a middle

Explain setViewControllers(_:animated:) and how it chooses one push or pop animation, and walk through how the bar picks its leading item from two navigation items.

for a senior

Spot the nil navigationController in a silent push, know when a custom leading item drops the Back behaviour, and rebuild a stack in one transition instead of chained pushes.

for a principal

Decide who is allowed to touch the stack: if every screen pushes freely, flows tangle; a team rule that one owner per flow edits the stack keeps Back behaviour predictable.

## The navigation stack A **`UINavigationController`** is a container view controller that manages an ordered array, `viewControllers`, called the **navigation stack**. The first element is the **root view controller**, the bottom of the stack; the last element is the **top view controller**, the one on screen. The controller also owns a navigation bar (and an optional toolbar) and updates them every time the top changes. A screen that is inside a navigation controller reaches it through its own `navigationController` property, which is `nil` otherwise. In a food-delivery app the Home tab might hold a stack of restaurant list, then menu, then dish. Each tap pushes one more screen; each Back tap pops one. ## Pushing - `pushViewController(_:animated:)` makes the passed view controller the new top and, when animated, slides it in horizontally. It also updates the navigation bar and toolbar for the new top. - `show(_:sender:)` on a navigation controller is interpreted as a push, which is what a Show segue calls. - The documentation lists two things you cannot push: a `UITabBarController`, and a view controller that is already on the stack. ## Popping - `popViewController(animated:)` removes the top screen and returns it. If the top is the root, it does nothing: the last item on the stack cannot be popped. - `popToViewController(_:animated:)` pops until the given screen is on top and returns the popped screens. - `popToRootViewController(animated:)` pops everything above the root, which is what "order placed, back to the restaurant list" needs. - Users pop too: by tapping Back, by swiping from the leading edge (`interactivePopGestureRecognizer`), or by long-pressing Back to pick an earlier screen from a list. ## What your code sees when a screen leaves Whoever pops, user or code, the same things happen. The navigation controller's delegate gets `navigationController(_:willShow:animated:)` and `navigationController(_:didShow:animated:)` for the new top. The popped screen gets its disappear callbacks, and inside them `isMovingFromParent` is `true`, which tells a pop apart from another screen being pushed on top. Two properties read the stack: - `topViewController` is the last element of `viewControllers`. - `visibleViewController` is the modally presented view controller if one exists, otherwise the top view controller. ## Replacing the whole stack `setViewControllers(_:animated:)` swaps the stack in one call. With animation on, UIKit runs a single transition chosen by the new top: a pop if it is already lower in the stack, a push if it is new, nothing if it is already on top. When that transition ends, the whole stack is the new array. It is the tool for rebuilding a stack at launch or jumping between distant screens without a chain of animations. ## Where the Back button comes from The leading button of the bar is decided by **two** navigation items: the new top's and the one underneath it. | Source | Set on | Effect | |---|---|---| | leading items (`leftBarButtonItem`, `leadingItemGroups`) | the top screen | replace the Back button, unless `leftItemsSupplementBackButton` is `true` | | `backBarButtonItem` | the screen underneath | used as the Back button; wins over `backButtonTitle` | | `backButtonTitle` (iOS 11) | the screen underneath | the Back button's title when that item is the bar's back item | | `title` | the screen underneath | the default title source | `backButtonDisplayMode` (iOS 14) controls how the title is chosen: `.default` uses an appropriate title, then falls back to a generic "Back", then to no title; `.generic` shows only the generic title; `.minimal` shows only the chevron. iOS 16 added `backAction`, which replaces the Back button's action without building a custom button. ## Common mistakes 1. Setting `backButtonTitle` on the screen being pushed: its value only appears when **another** screen is pushed above it. 2. Calling `dismiss(animated:completion:)` to go back one level: that undoes a modal presentation, not a push. 3. Replacing the Back button with `leftBarButtonItem` and forgetting that it no longer pops by itself. 4. Calling `navigationController?.pushViewController(_:animated:)` from a screen that is not inside a navigation controller: the optional chain is `nil` and nothing happens. Interviewers ask this to check that you see the stack as data UIKit manages for you, and that you know which screen owns which piece of the bar.

  • How does setViewControllers(_:animated:) decide which animation to run?
    It looks at the last view controller in the new array. If it is already on the stack but not on top, UIKit runs a pop; if it is already the top, no transition runs; if it is not on the stack, a push. Only one transition runs, and when it finishes the entire stack is the new array, so A, B, C set to D, A, B animates a pop and ends as D, A, B.
  • A button calls navigationController?.pushViewController(_:animated:) and nothing happens; what do you check first?
    Whether the screen is inside a navigation controller at all. A view controller presented on its own, or set directly as the window's root, has a nil navigationController, and the optional chain silently skips the push. Wrap it in a UINavigationController before presenting, or push from a screen that is already in one.

  • Sets backButtonTitle on the pushed screen to rename that screen's own Back button.
  • Believes popViewController(animated:) can remove the root view controller.
  • Uses dismiss(animated:completion:) to go back one screen in a navigation stack.
  • Pushes a UITabBarController onto an existing navigation stack.
  • Sets a leftBarButtonItem and expects the system Back button to stay beside it.
open as a page

In UIKit, how should a UITabBarController hold Home, Orders and Account tabs so that each tab keeps its own navigation stack?

level: middleimportance: must knowfreq 68%

basics

~20 s

Give each tab its own UINavigationController as its root and keep the UITabBarController outside every navigation stack. Each tab then pushes and pops independently; iOS 18 adds UITab objects as another way to declare tabs.

open as a page

In UIKit, how do you present a basket screen as a sheet that rests at half height and expands to full height with UISheetPresentationController?

level: middleimportance: should knowfreq 62%

basics

~10 s

Present the basket in a sheet style (pageSheet or formSheet) and, before calling present, set its sheetPresentationController's detents to [.medium(), .large()]. Add prefersGrabberVisible for a grabber and selectedDetentIdentifier to choose the starting height.

open as a page

In UIKit, a custom push animator grows a dish photo into its detail screen, but afterwards the app is stuck and Back does nothing; what is wrong, and how should the animator work?

level: seniorimportance: should knowfreq 32%

basics

~10 s

The animator never called completeTransition(_:) on its transition context, so UIKit never finished the push. An animator adds the incoming view to containerView, animates it, and ends every path with completeTransition(!transitionWasCancelled).

open as a page

In UIKit, after a user swipes a basket page sheet away, the menu screen's viewWillAppear never runs and its basket badge stays stale; why, and how do you fix it?

level: seniorimportance: should knowfreq 50%

basics

~20 s

A page or form sheet leaves the menu visible, so UIKit never sends it viewWillDisappear or viewDidDisappear and has no reappearance to report. Refresh in presentationControllerDidDismiss(_:) for swipes and in your dismiss completion for code.

open as a page

As a lead on a UIKit app, would you let view controllers push their own next screen or make coordinators own each flow, and what does the coordinator rule cost?

level: principalimportance: should knowfreq 45%

basics

~20 s

Self-routing screens are simplest for small apps but hard-wire each flow into its view controllers. Coordinators own the UINavigationController and choose the next screen, making flows reusable, but must track Back pops and their own lifetime.

open as a page