Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict sheet dismissing by swipe down in swiftui

Tags:

ios

swift

swiftui

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.

like image 862
Azhagusundaram Tamil Avatar asked Oct 11 '19 15:10

Azhagusundaram Tamil


2 Answers

iOS 14: workaround -> fullScreenCover

fullScreenCover(isPresented:onDismiss:content:)

https://developer.apple.com/documentation/swiftui/view/fullscreencover(ispresented:ondismiss:content:)

iOS 15: interactiveDismissDisabled

.interactiveDismissDisabled(false)

https://developer.apple.com/documentation/swiftui/path/interactivedismissdisabled(_:)

like image 173
hstdt Avatar answered Oct 04 '22 01:10

hstdt


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 }
        }
    }
}
like image 23
swift code Avatar answered Oct 04 '22 03:10

swift code