Modal presentation styles

UIKit Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Predict the output

Screen A presents B with the default style on iOS 17. Which A callback runs?

let b = DetailViewController()
present(b, animated: true)   // modalPresentationStyle left as .automatic
Options
Hint 1 of 1

What is the default style since iOS 13?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: B Neither: A stays in the hierarchy behind the page sheet

Correct. Use presentation controller delegate callbacks instead.

Why not the others?
  • A A.viewWillDisappear and A.viewDidDisappear. Only full-screen styles remove A's view.
  • C Only A.viewDidDisappear. Both or neither run; here it is neither.
  • D A.viewDidLoad again. viewDidLoad runs once per view load.

30-second answer

Since iOS 13 the default is .automatic, usually a page sheet that leaves the presenting screen visible behind it. Because the presenter stays in the hierarchy, its viewWillDisappear and viewDidDisappear aren't called. .fullScreen removes the presenter's view and does call them. Sheets can be swiped down unless isModalInPresentation is true.

Practise saying it · 30 s. No recording, just a timer

Explain modal presentation styles.

Say your answer out loud, then check which of these points you covered:

  • Default page sheet
  • Presenter callbacks differ
  • Detents and dismissal control

Deep dive

sheetPresentationController gives detents (.medium, .large, custom).

Use presentationControllerDidDismiss to react to a swipe-down.

.overFullScreen keeps the presenter visible underneath, for transparent overlays.

Popovers adapt to sheets on compact width.

Common mistakes

  • Relying on the presenter's viewWillAppear after a sheet dismisses.
  • Blocking swipe-to-dismiss without a reason.

Senior take & follow-ups

Seniors know which lifecycle callbacks fire for each style.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you stop swipe-down when there are unsaved changes?

    Show hint

    isModalInPresentation plus presentationControllerDidAttemptToDismiss.

  2. How do you make a half-height sheet?

    Show hint

    Detents on sheetPresentationController.

From the author

Catalog - UI

Made by the author: browse UIKit components with live demos, and copy their code snippets into your project.

Suggest a fix if something here is wrong or out of date.