In UIKit, how do you present a basket screen as a sheet that rests at half height and expands to full height with UISheetPresentationController?
answer
- a sheet style, not full screen
- configure before calling present
- detents ordered smallest to largest
- medium is inactive in compact height
- largestUndimmedDetentIdentifier keeps the menu usable
basics
~10 sPresent 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.
solid answer
~40 s`sheetPresentationController` is non-nil only when the presented view controller's `modalPresentationStyle` is `.pageSheet` or `.formSheet`; the default `.automatic` resolves to `.formSheet` from iOS 18 and to `.pageSheet` before, so a default presentation has one too. Before calling `present(_:animated:completion:)`, set `detents = [.medium(), .large()]` (at least one, smallest first; the default is only `.large()`), `prefersGrabberVisible = true`, and `selectedDetentIdentifier` if it shouldn't open at the smallest detent. `largestUndimmedDetentIdentifier = .medium` stops the dimming at half height so the menu stays visible and usable. The medium detent is inactive in compact height, such as an iPhone in landscape. iOS 16 added `.custom(identifier:resolver:)` for a height you compute, and the delegate's `sheetPresentationControllerDidChangeSelectedDetentIdentifier(_:)` reports drags between detents.
code
swift · 26 linesimport UIKit
final class BasketViewController: UIViewController {}
final class MenuViewController: UIViewController, UISheetPresentationControllerDelegate {
func showBasket() {
let basket = BasketViewController()
basket.modalPresentationStyle = .pageSheet // any sheet style works; this one is explicit
if let sheet = basket.sheetPresentationController {
sheet.detents = [.medium(), .large()] // smallest to largest
sheet.selectedDetentIdentifier = .medium
sheet.largestUndimmedDetentIdentifier = .medium // menu stays usable behind it
sheet.prefersGrabberVisible = true
sheet.delegate = self
}
present(basket, animated: true)
}
func sheetPresentationControllerDidChangeSelectedDetentIdentifier(
_ sheetPresentationController: UISheetPresentationController
) {
// Runs when the user drags between detents, not when code sets the identifier.
let expanded = sheetPresentationController.selectedDetentIdentifier == .large
print("basket expanded:", expanded)
}
}go deeper
Recall the recipe: a sheet style, sheetPresentationController, detents medium and large, then present. Know that the grabber is off by default.
Explain when sheetPresentationController is nil, why detents go smallest first, what a nil selectedDetentIdentifier shows, and what largestUndimmedDetentIdentifier changes behind the sheet.
Plan for compact height where medium is inactive, animate detent changes with animateChanges, and keep custom detent resolvers pure or invalidate them when their inputs change.
Decide when a non-modal half-height sheet beats pushing a screen, and set a team default for grabbers, dimming and landscape behaviour so sheets feel the same across features.
## What a sheet is in UIKit A **sheet** is a modal presentation that slides up from the bottom and leaves part of the screen behind it visible. In UIKit you get one by presenting a view controller whose **`modalPresentationStyle`** is `.pageSheet` or `.formSheet`. For those two styles the presented view controller's **`sheetPresentationController`** property (iOS 15) holds a **`UISheetPresentationController`**, the object that sizes and positions the sheet; for any other style the property is `nil`. The default style, `.automatic`, resolves to `.formSheet` for ordinary view controllers from **iOS 18**, and to `.pageSheet` on earlier releases. On an iPhone in portrait both show as a sheet with the content behind visible near the top, so a basket presented with the default style already has a sheet presentation controller. ## Detents: where the sheet rests A **detent** is a height at which the sheet naturally rests. You list them in `detents`: - `.medium()` is roughly half the screen and is **inactive in compact height**, such as an iPhone in landscape. - `.large()` is full height, and the default value of `detents` is `[.large()]` alone. - The array must contain at least one detent and must be ordered **from smallest to largest**. - `selectedDetentIdentifier` picks the current detent; when it is `nil` or not found in the array, the sheet shows at the **smallest** detent. From **iOS 16**, `.custom(identifier:resolver:)` creates a detent whose resolver returns a height inside the sheet's safe area, or `nil` to make it inactive. The resolver gets a context with `maximumDetentValue` and the container's traits. If the resolver depends on outside state, such as the number of items in the basket, call `invalidateDetents()` when that state changes. ## The basket sheet, step by step 1. Create the basket view controller. 2. Keep or set a sheet style; `.pageSheet` makes the intent explicit. 3. Read `sheetPresentationController` and set `detents`, `selectedDetentIdentifier` and `prefersGrabberVisible` (the grabber is hidden by default). 4. Set `largestUndimmedDetentIdentifier = .medium` so that at half height the menu behind is not dimmed and stays usable; when it is `nil`, every detent is dimmed. 5. Call `present(_:animated:completion:)`. You configure **before** presenting so the sheet appears at the right height; the documentation says you can also adjust it after it is on screen. ## Page sheet or form sheet On iPhone in portrait the two styles look alike; they differ where there is room, on iPad and in regular width. **`prefersPageSizing`** (iOS 17) on the sheet presentation controller states the difference directly: - `true` maps to the page-sheet behaviour: the sheet's width follows the readable width. - `false` maps to the form-sheet behaviour: the sheet's size follows the presented view controller's `preferredContentSize`. - The default is `false` from iOS 18 and `true` on earlier releases, matching the change in what `.automatic` resolves to. So a basket that must look the same on iOS 17 and iOS 18 iPads sets the style, or `prefersPageSizing`, explicitly instead of relying on the default. ## Changing and observing the detent | Need | API | |---|---| | move the sheet in code, animated | set `selectedDetentIdentifier` inside `animateChanges(_:)` | | learn the user dragged to another detent | `sheetPresentationControllerDidChangeSelectedDetentIdentifier(_:)` on the delegate | | stop scrolling inside the sheet from expanding it | `prefersScrollingExpandsWhenScrolledToEdge = false` | | keep the sheet edge-attached in landscape instead of full screen | `prefersEdgeAttachedInCompactHeight = true` | The delegate callback runs only for **user interaction**; setting `selectedDetentIdentifier` in code does not call it, so code that changes the detent updates its own state. ## Mistakes to avoid - Setting `modalPresentationStyle = .fullScreen` and then wondering why `sheetPresentationController` is `nil`. - Listing `[.large(), .medium()]`, against the smallest-first rule. - Designing the basket so it works only at medium height, then rotating an iPhone to landscape where medium is inactive. - Using a custom detent on an iOS 15 deployment target without an availability check. - Expecting the detent-change delegate method after a programmatic change.
- How do you move the basket sheet from medium to large in code, with animation?Set sheetPresentationController.selectedDetentIdentifier = .large inside sheet.animateChanges { }. Changes made inside that block animate, and by the time it returns the sheet stack has been laid out. The delegate's detent-change method is not called for this change, so update any state that depends on the detent yourself.
- When does a custom detent's resolver need invalidateDetents()?When its height depends on something outside the resolver's context, such as how many items the basket holds. invalidateDetents() tells the sheet to re-evaluate the detent in its next layout pass; call it when that input changes, inside animateChanges(_:) to animate the new height. A resolver that reads only the context needs no call.
- Expects detents to apply when modalPresentationStyle is .fullScreen.
- Lists detents largest first, as [.large(), .medium()].
- Expects the medium detent on an iPhone held in landscape.
- Waits for the detent-change delegate call after setting selectedDetentIdentifier in code.
- Uses .custom(identifier:resolver:) on an iOS 15 deployment target without a check.
Assumes the iOS 27.1 SDK; medium and large detents are iOS 15, custom detents iOS 16, and .automatic resolves to formSheet from iOS 18 (pageSheet before).