Restrict sheet dismissing by swipe down in swiftui

Viewed 4199

I have a button and when I click on it a new View will present as a sheet. In SwiftUI by default swipe down gesture will dismiss the sheet view. I want to restrict it.

I'll have a button for dismissing. Until I press that button sheet should not get dismissed.

3 Answers

We have created a SwiftUI style extension, at https://gist.github.com/mobilinked/9b6086b3760bcf1e5432932dad0813c0

/// Example:
struct ContentView: View {
    @State private var presenting = false
    
    var body: some View {
        VStack {
            Button {
                presenting = true
            } label: {
                Text("Present")
            }
        }
        .sheet(isPresented: $presenting) {
            ModalContent()
                .allowAutoDismiss { false }
        }
    }
}
Related