How it works
A PresentationDetent describes a height at which a presented sheet comes to rest, letting a sheet stop at a partial size instead of always covering the full screen. Reach for it when you want a sheet that a person can drag between a compact, content-peeking state and a fully expanded one, while still seeing the view behind it. SwiftUI ships a set of standard detents as type properties, and you supply them to a sheet to define its allowed resting positions.
Use the standard detents as type properties
PresentationDetentexposes its common positions as static members rather than something you construct by hand..mediumrests the sheet at roughly half the available height, and.largeexpands it to the full sheet height — the two values passed in this example.Declare the allowed positions with presentationDetents(_:)
You attach detents to a sheet's content with the
presentationDetents(_:)modifier, which takes a set ofPresentationDetentvalues describing every height the sheet is permitted to rest at. Here.presentationDetents([.medium, .large])gives the sheet two stops, so it opens partway and can be dragged up to full height.Order doesn't pick the initial height — the set does
Because the argument is a
Set, the collection only defines which detents exist, not a sequence; the sheet starts at the smallest detent in the set. Listing.mediumand.largemeans the sheet appears at the medium height first, with the large height available by dragging.Pair it with presentationDragIndicator(_:) so the resize affordance is visible
Detents are most discoverable when the sheet shows a grabber the person can pull.
.presentationDragIndicator(.visible)draws that handle, signaling that the sheet defined by thePresentationDetentset can move between its medium and large rest heights.Custom heights with .height(_:) and .fraction(_:)
Beyond the standard cases,
PresentationDetentoffers.height(_:)for a fixed point value and.fraction(_:)for a proportion of the container, so you can define your own resting positions — for instance swapping.mediumfor.fraction(0.3)in the set passed topresentationDetents(_:).
.presentationDetents([.medium, .large]) to .presentationDetents([.height(120), .large]) and watch the sheet open at a fixed 120-point peek before it can be dragged to full height.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct PresentationDetentDemo: View {
@State private var showSheet = true
var body: some View {
Text("Detail behind the sheet")
.padding()
.sheet(isPresented: $showSheet) {
VStack(spacing: 12) {
Text("Medium Detent Sheet")
.font(.headline)
Text("Resting at half height.")
.foregroundStyle(.secondary)
}
.padding()
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
}
}
}