I'm using a custom modifier called AutoSheetDetentModifier to automatically size a sheet based on its content.
On iOS 26, it works as expected: the content height is measured correctly and the sheet shrinks to match that height.
However, on iOS 16, 17 and 18, the same code doesn’t work. The content height is still measured, but the sheet does not reduce its height. Instead, the sheet remains larger and the content appears vertically centered. (Note that content() includes ScrollView)
public struct AutoSheetDetentModifier: ViewModifier {
@State private var height: CGFloat = 380 // default value to avoid bouncing
public func body(content: Content) -> some View {
content
.modifier(MeasureHeightViewModifier(height: $height))
.presentationDetents([.height(height)])
}
}
public struct MeasureHeightViewModifier: ViewModifier {
@Binding var height: CGFloat
public func body(content: Content) -> some View {
content
.fixedSize(horizontal: false, vertical: true)
.background(
GeometryReader { geo -> Color in
DispatchQueue.main.async {
height = geo.size.height
}
return Color.clear
}
)
}
}
extension View {
public func applyAutoSheetDetent() -> some View {
self
.modifier(AutoSheetDetentModifier())
}
}
public var body: some View {
VStack {
header()
content() // includes ScrollView
footer()
}
.background(Color.customGray)
.applyAutoSheetDetent()
}
func content() -> some View {
ScrollView {
VStack {
ForEach(items) { item in
itemRow(item)
}
}
}
.frame(maxHeight: UIScreen.main.bounds.height * 0.7)
}
Screenshot from iOS 26 (working as expected):
Screenshot from iOS 18 (not working):
How can I make .presentationDetents(.height) shrink the sheet correctly on iOS 16–18, the same way it does on iOS 26?