HomeiOS DevelopmentSwiftUI (sheets) - presentationDetents behaves incorrectly on iOS 16–18 however works accurately...

SwiftUI (sheets) – presentationDetents behaves incorrectly on iOS 16–18 however works accurately on iOS 26


I am utilizing a customized modifier referred to as AutoSheetDetentModifier to routinely measurement a sheet primarily based on its content material. (in SwiftUI)

On iOS 26, it really works as anticipated: the content material top is measured and the sheet shrinks to match that top.

Nonetheless, on iOS 16, 17 and 18, the identical code doesn’t work.
The content material top continues to be measured, however the sheet doesn’t scale back its top. As a substitute, the sheet stays bigger and the content material seems vertically centered.

public struct AutoSheetDetentModifier: ViewModifier {
    @State non-public var top: CGFloat = 380 // default worth to keep away from bouncing

    public func physique(content material: Content material) -> some View {
        content material
            .modifier(MeasureHeightViewModifier(top: $top))
            .presentationDetents([.height(height)])
    }
}

public struct MeasureHeightViewModifier: ViewModifier {
    @Binding var top: CGFloat
    
    public func physique(content material: Content material) -> some View {
        content material
            .fixedSize(horizontal: false, vertical: true)
            .background(
                GeometryReader { geo -> Coloration in
                    DispatchQueue.primary.async {
                        top = geo.measurement.top
                    }
                    return Coloration.clear
                }
            )
    }
}

extension View {
    public func applyAutoSheetDetent() -> some View {
        self
            .modifier(AutoSheetDetentModifier())
    }
}
public var physique: some View {
     VStack {
         header()
         content material()
         footer()
     }
     .background(Coloration.customGray)
     .applyAutoSheetDetent()
}

Screenshot from iOS 26 (working as anticipated):

SwiftUI (sheets) – presentationDetents behaves incorrectly on iOS 16–18 however works accurately on iOS 26

Screenshot from iOS 18 (not working):

enter image description here

How can I make .presentationDetents(.top) shrink the sheet accurately on iOS 16–18, the identical means it does on iOS 26?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments