21.1 C
Canberra
Sunday, December 14, 2025

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


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

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

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

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

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

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

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

Screenshot from iOS 26 (working as anticipated):

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

Screenshot from iOS 18 (not working):

enter image description here

How can I make .presentationDetents(.peak) shrink the sheet appropriately on iOS 16–18, the identical manner it does on iOS 26?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles