Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent SwiftUI View in background shrinking on overriding View in .sheet(isPresented:) become almost full screen?

Tags:

swift

swiftui

I want on opening max height content with .sheet(isPresented:), not to animate and shrink content behind (in red rectangle). Is it possible?

enter image description here

like image 228
Konstantin.Efimenko Avatar asked Sep 02 '25 09:09

Konstantin.Efimenko


1 Answers

The background view gets smaller when the presentation detent is too large. You can see that the background view does not get smaller when using a .medium detent for example.

Presumably there is a threshold for presentation detents, and if you go above that threshold, the background view gets smaller. You can set the detent to something just below that threshold.

What I've found to work is .fraction(0.999), or a custom detent whose height is 1 less than the maximum height.

struct ContentView: View {
    @State var x = false
    var body: some View {
        Button("Click") {
            x = true
        }
        .sheet(isPresented: $x) {
            Text("Foo")
                .presentationDetents([
                    .custom(CustomDetent.self) // or .fraction(0.999)
                ])
        }
    }
}

struct CustomDetent: CustomPresentationDetent {
    static func height(in context: Context) -> CGFloat? {
        return context.maxDetentValue - 1
    }
}
like image 86
Sweeper Avatar answered Sep 05 '25 02:09

Sweeper