SwiftUI: Dynamically changing sheet detent without a resize option

Dear Community,

I have a use case where I have to present multiple sheets of different, non-resizable heights from a View.

It seems like when multiple .presentationDetents() are specified and you control the current detent with selection:, SwiftUI always allows the user to resize the sheet, even if .presentationDragIndicator(.hidden) is set.

In my use case, however, I wouldn't like to let the user do so, because this would result in a small view presented on a large sheet with lot of empty space around.

Specifying just one presentationDetent and changing it or its height programmatically doesn't seem to work: the change does not reflect immediately, even if the view in the sheet is given a new id() every time.

The only thing I could come up with is a custom detent as follows:

import SwiftUI

public class DynamicDetent: CustomPresentationDetent {

    static var height: CGFloat = 80

    public static func height(in context: Context) -> CGFloat? {
        return height
    }
}

extension PresentationDetent {
    static let dynamic = Self.custom(DynamicDetent.self)
}

As the height() functon is being called every time the sheet is being displayed, setting DynamicDetent.height before displaying a new sheet works. Is there any other, less hacky way to achieve the same result?

SwiftUI: Dynamically changing sheet detent without a resize option
 
 
Q