skip to content

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%

answer

  1. did the menu ever disappear
  2. the background stays visible under a sheet
  3. a delegate on the presentation controller
  4. user dismissals only, not programmatic ones
  5. presentationControllerDidDismiss after the animation

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.

solid answer

~40 s

The documentation for `.pageSheet` and `.formSheet` says that where the background content remains visible, UIKit doesn't call the presenting view controller's `viewWillDisappear(_:)` and `viewDidDisappear(_:)`. The menu never left, so no `viewWillAppear(_:)` follows when the sheet goes; code written for full-screen modals, where the presenter's views are removed, loses its refresh. The fix is to listen to the presentation: before presenting, set the basket's `presentationController?.delegate` to the menu and refresh in `presentationControllerDidDismiss(_:)`, which runs after a user dismissal finishes. It is not called when code calls `dismiss(animated:completion:)`, so that path refreshes in the completion handler or through the basket's own delegate or closure. To protect unsaved input, set `isModalInPresentation` and answer `presentationControllerDidAttemptToDismiss(_:)` with an alert.

code

swift · 32 lines
swift
import UIKit

final class BasketViewController: UIViewController {
    var hasUnsavedNote = false {
        didSet { isModalInPresentation = hasUnsavedNote } // blocks swipe-to-dismiss only
    }
}

final class MenuViewController: UIViewController, UIAdaptivePresentationControllerDelegate {
    func showBasket() {
        let basket = BasketViewController()
        basket.presentationController?.delegate = self
        present(basket, animated: true)
    }

    // Called after a user-initiated dismissal finishes; not after dismiss(animated:completion:).
    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        refreshBasketBadge()
    }

    // Called when a swipe is refused because isModalInPresentation is true.
    func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
        let alert = UIAlertController(title: "Discard your note?", message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "Discard", style: .destructive) { [weak self] _ in
            self?.dismiss(animated: true) { self?.refreshBasketBadge() }
        })
        alert.addAction(UIAlertAction(title: "Keep Editing", style: .cancel))
        presentationController.presentedViewController.present(alert, animated: true)
    }

    func refreshBasketBadge() {}
}

go deeper

for a junior

Recall that a sheet leaves the screen behind it visible, so that screen does not get its disappear and appear callbacks when the sheet comes and goes.

for a middle

Explain the presentation controller's delegate, which of its methods run for swipes, and why a Done button that calls dismiss needs its own refresh path.

for a senior

Diagnose stale state after a sheet from the callback table, cover both dismissal paths, and guard unsaved input with isModalInPresentation and an alert on the refused swipe.

for a principal

Set a rule that presented screens report results through an explicit callback rather than presenters re-reading state on appearance, so the presentation style can change without breaking data flow.

## What the user sees and what UIKit sends In a food-delivery app the menu presents the basket as a sheet. The user removes an item, swipes the sheet down, and the badge on the menu still shows the old count, because the badge was refreshed in the menu's `viewWillAppear(_:)`. The **appearance callbacks** (`viewWillAppear(_:)`, `viewDidAppear(_:)`, `viewWillDisappear(_:)`, `viewDidDisappear(_:)`) describe a view controller's view entering or leaving the screen. For the built-in styles, whether the **presenting** view controller gets them depends on whether its view goes away: | Presentation style | Presenter's views | Presenter's disappear and appear pair | |---|---|---| | `.fullScreen` | removed after the presentation completes | sent | | `.pageSheet`, `.formSheet` where the background stays visible | stay on screen | not sent | | `.pageSheet`, `.formSheet` in compact height | behave like `.fullScreen` | follow the full-screen behaviour | The documentation for both sheet styles says it directly: where the background content remains visible, UIKit doesn't call the presenting view controller's `viewWillDisappear(_:)` and `viewDidDisappear(_:)`. Since the menu never disappeared, there is no reappearance when the sheet closes. Since iOS 13 the default style, `.automatic`, has resolved to a sheet style for ordinary view controllers, so this bites code that never set a style at all. ## Listening to the presentation instead The object that knows about the dismissal is the **presentation controller**. Every presented view controller has a `presentationController`, whose delegate conforms to **`UIAdaptivePresentationControllerDelegate`**. Set the delegate before calling `present(_:animated:completion:)`. - `presentationControllerShouldDismiss(_:)` asks whether a user dismissal may happen; return `false` to refuse. It may be called at any time, so it should be fast. - `presentationControllerWillDismiss(_:)` runs when the user acts to dismiss, before interaction or animations begin, the place to animate alongside it. - `presentationControllerDidDismiss(_:)` runs when the user has dismissed the presentation, **after all animations are finished**. This is where the badge refresh goes. - `presentationControllerDidAttemptToDismiss(_:)` runs when a user dismissal was refused, so you can tell the user why. The will and did methods are **not called when the presentation is dismissed programmatically**. ## Covering the programmatic path A Done button that calls `dismiss(animated:completion:)` takes the other path. Calling it on the presented basket is fine: UIKit asks the presenting view controller to handle it. Refresh in the **completion handler**, which runs after the dismissed controller's `viewDidDisappear(_:)`, or have the basket report "done" through a delegate or closure that the menu handles. A robust screen has both: 1. Swipe down: `presentationControllerDidDismiss(_:)`. 2. Done button: the completion handler or the basket's callback. ## Detecting it from the presented side The basket itself still gets its own appearance callbacks, because its view really does leave the screen. Inside `viewDidDisappear(_:)`, `isBeingDismissed` is `true` when the basket has been dismissed, whether by a swipe or by code; the header suggests checking `isBeingDismissed || isMovingFromParent` to tell a dismissal or pop from other disappearances. That makes the basket a reliable place to send its result, for example by calling an `onClose` closure the menu supplied, which covers swipes and Done taps with one code path. Once presented, `activePresentationController` (iOS 16) returns the controller actually in use, including an adapted one. ## Protecting unsaved input If the basket has an unsaved delivery note, set **`isModalInPresentation`** to `true` while it is dirty. UIKit then prevents interactive dismissal and ignores events outside the sheet. When the user tries to swipe anyway, `presentationControllerDidAttemptToDismiss(_:)` runs, and the header suggests telling the user why, for example with a `UIAlertController` that offers Discard. `isModalInPresentation` does not stop `dismiss(animated:completion:)` from code. ## Fixes that cost more than they save - Switching every sheet to `.fullScreen` to get `viewWillAppear(_:)` back gives up the swipe gesture and the visible context behind the sheet. - Polling the basket from a timer hides the missing event instead of handling it. The senior signal is naming the cause precisely: not a UIKit bug, but appearance callbacks reporting exactly what happened on screen.

  • How do you ask before discarding a delivery note when the user swipes the basket down?
    Set isModalInPresentation to true while the note is unsaved, or return false from presentationControllerShouldDismiss(_:). The swipe is then refused and presentationControllerDidAttemptToDismiss(_:) runs; present a UIAlertController from the basket offering Discard or Keep Editing, and dismiss in code if the user discards.
  • Which presentations still send the presenter its disappear callbacks?
    A .fullScreen presentation removes the presenter's views after it completes, so the presenter gets its disappear pair and later its appear pair. Page and form sheets skip them wherever the background stays visible; in compact height they behave like full screen. The rule follows what is on screen, not the style's name.

  • Relies on the presenter's viewWillAppear to notice a page sheet closing.
  • Expects presentationControllerDidDismiss(_:) after calling dismiss(animated:completion:) in code.
  • Thinks isModalInPresentation also blocks programmatic dismissal.
  • Assumes the presenting screen disappears under every modal presentation style.
  • Switches every sheet to fullScreen just to get viewWillAppear back.