skip to content

In SwiftUI, how do presentationDetents and presentationDragIndicator make a sheet rest at half height, and where do you apply them?

level: middleimportance: should knowfreq 50%

answer

  1. heights where a sheet rests
  2. configures the enclosing sheet
  3. medium, large, fraction, height
  4. selection binding must match the set

basics

~10 s

Apply presentationDetents([.medium, .large]) to the view inside the sheet's content closure; the sheet then rests at those heights and people drag between them. presentationDragIndicator(.visible) shows the grabber. Both need iOS 16.

solid answer

~40 s

A **detent** is a height where a sheet naturally rests. By default a sheet supports only `.large`. `presentationDetents(_:)` (iOS 16) takes a `Set<PresentationDetent>`: `.medium`, `.large`, `.fraction(_:)`, `.height(_:)` or `.custom(_:)` with a `CustomPresentationDetent` type. With more than one detent, people can drag to resize. The modifier sets the detents of the **enclosing** sheet, so it goes on the view returned from the `sheet` content closure, not on the presenter. `presentationDetents(_:selection:)` adds a `Binding<PresentationDetent>` to read or change the current height; its value must be one of the listed detents. `presentationDragIndicator(.visible)` shows the grabber, and from iOS 16.4 `presentationBackgroundInteraction(.enabled(upThrough: .medium))` lets people keep using the list behind a half-height card.

code

swift · 16 lines
swift
struct ContactCard: View {
    let name: String
    @State private var detent = PresentationDetent.medium

    var body: some View {
        VStack(spacing: 12) {
            Text(name).font(.title2)
            if detent == .large {
                Text("Full details and edit controls")
            }
            Button("Edit") { detent = .large }
        }
        .presentationDetents([.medium, .large], selection: $detent)
        .presentationDragIndicator(.visible)
    }
}

go deeper

for a junior

Recall that presentationDetents([.medium, .large]) inside the sheet's content makes it rest at half height, and presentationDragIndicator(.visible) shows the grabber.

for a middle

Explain that the modifier configures the enclosing sheet, list the detent kinds, and show the selection binding moving the sheet, with its rule that the value must be in the set.

for a senior

Show you design the interaction, not just the height: background interaction up through a detent, scroll versus resize behaviour, and expanding to large when editing begins.

for a principal

Weigh whether a resizable sheet is the right container at all for the flow, against a pushed screen or a full-screen task, and keep detent choices consistent across a team's screens.

## What a detent is Apple's documentation defines a **detent** (`PresentationDetent`, iOS 16) as a height where a sheet naturally rests. Without any detent modifier, a sheet supports only the **large** detent: the familiar full-height card. Detents turn the sheet into something closer to a resizable drawer, which suits a contacts app that previews a contact in a half-height card and expands it to full height for editing. ## Declaring the heights `presentationDetents(_ detents: Set<PresentationDetent>)` lists every height the sheet may rest at. If you provide more than one, people can drag the sheet to resize it. | Detent | How you write it | Where the sheet rests | |---|---|---| | Large | `.large` | full sheet height, the default | | Medium | `.medium` | roughly half height | | Fraction | `.fraction(0.3)` | that fraction of the available height | | Height | `.height(120)` | a fixed height in points | | Custom | `.custom(MyDetent.self)` | whatever `height(in:)` on your `CustomPresentationDetent` type returns | A custom detent's `height(in:)` receives a `PresentationDetent.Context` that exposes `maxDetentValue` and environment values, so it can compute a height from, say, the Dynamic Type size. ## Where the modifiers go The documentation says `presentationDetents` sets the detents of the **enclosing sheet**. That decides placement: 1. Put it on the view returned from the `sheet` content closure (or on any view inside it), because that view is enclosed by the sheet you mean. 2. Do not put it on the button or screen that presents the sheet: that view is not inside this sheet, so it does not shape this sheet. 3. Apply the companion modifiers (`presentationDragIndicator`, `presentationBackgroundInteraction` and the rest of the `presentation…` family) in the same place, inside the content. ## Controlling the current height `presentationDetents(_:selection:)` takes a `Binding<PresentationDetent>`. Reading it tells you where the sheet currently rests; writing it moves the sheet. The documentation's one rule: the bound value must match one of the detents in the set. Typical uses: - Start a contact preview at `.medium`, then set the selection to `.large` when the person taps Edit, so the form gets room for the keyboard. - Show extra detail only when the selection is `.large`, hiding it at `.medium`. Resizing does not change the presentation binding: a sheet at `.medium` is still presented, and the Bool or item that drives it is untouched. ## The grabber, and the content behind - **`presentationDragIndicator(_:)`** (iOS 16) takes a `Visibility`: `.automatic`, `.visible` or `.hidden`. The documentation says you can show it when it is not apparent that the sheet can resize, or when the sheet cannot be dismissed interactively. - **`presentationBackgroundInteraction(_:)`** (iOS 16.4). On many platforms SwiftUI disables the view behind a sheet until it is dismissed. `.enabled` lets people interact with that view, and `.enabled(upThrough: .medium)` allows it only while the sheet rests at or below the medium detent. - **`presentationContentInteraction(_:)`** (iOS 16.4) takes `.automatic`, `.resizes` or `.scrolls`: whether a swipe on scrollable sheet content resizes the sheet first or scrolls the content. - **`presentationCornerRadius(_:)`** and **`presentationBackground(_:)`** (iOS 16.4) style the card itself. - **`presentationSizing(_:)`** (iOS 18) is a different axis: it picks a built-in size such as `.form`, `.page` or `.fitted` for sheets on larger screens, and does not replace detents. ## Putting it together ```swift struct Contact: Identifiable { let id = UUID(); var name: String } struct ContactsList: View { let contacts: [Contact] @State private var previewed: Contact? @State private var detent = PresentationDetent.medium var body: some View { List(contacts) { contact in Button(contact.name) { previewed = contact } } .sheet(item: $previewed) { contact in Text(contact.name) .presentationDetents([.medium, .large], selection: $detent) .presentationDragIndicator(.visible) .presentationBackgroundInteraction(.enabled(upThrough: .medium)) } } } ``` The list stays usable behind the half-height card, the grabber signals that the card resizes, and the `detent` state lets code expand the card when editing starts. ## What interviewers probe - **Placement**: candidates who attach detents to the presenter are usually wrong about which sheet they configure. - **Defaults**: only `.large` without the modifier. - **Release lines**: detents and the drag indicator are iOS 16; background and content interaction, corner radius and compact adaptation are iOS 16.4; `presentationSizing` is iOS 18. With a deployment target of iOS 17, everything here except `presentationSizing` is available without an availability check.

  • How do you let people keep scrolling the list behind a half-height sheet?
    Apply `presentationBackgroundInteraction(.enabled(upThrough: .medium))` inside the sheet, available from iOS 16.4. SwiftUI normally disables the view behind a sheet on many platforms; this re-enables it only while the sheet rests at or below the medium detent, so the list is usable under a preview card but blocked once the sheet is expanded.
  • When would you write a CustomPresentationDetent instead of using .fraction or .height?
    When the height must be computed at presentation time from context, for example from Dynamic Type or the maximum available height. A type conforming to `CustomPresentationDetent` implements `static func height(in context:)`; the context exposes `maxDetentValue` and environment values. You then list it as `.custom(MyDetent.self)` alongside the built-in detents.

  • presentationDetents goes on the button or screen that presents the sheet.
  • Without the modifier a sheet supports both medium and large detents.
  • The selection binding can hold a detent that is not in the set.
  • Dragging a sheet down to the medium detent dismisses it and resets the Bool.
  • presentationSizing replaces detents for setting an iPhone sheet's height.