7

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.

Azhagusundaram Tamil
  • 2,053
  • 3
  • 21
  • 36
  • 1
    I don't think you can - it's *not* a SwiftUI thing, it's an iOS 13 thing. Now, if there's a SwiftUI modifier for UIKit's `modalPresentationStyle`... and there may be... you're in business. https://sarunw.com/posts/modality-changes-in-ios13/?utm_campaign=iOS%2BDev%2BWeekly&utm_medium=email&utm_source=iOS%2BDev%2BWeekly%2BIssue%2B420 My point? It's iOS 13, not SwiftUI. Start looking there. –  Oct 11 '19 at 18:39
  • If you get it working, post it as an answer here - I'll definitely upvote it. –  Oct 15 '19 at 10:05

3 Answers3

16

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(_:)

hstdt
  • 5,652
  • 2
  • 34
  • 34
5

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 }
        }
    }
}
swift code
  • 115
  • 1
  • 1
3

If you are using swiftUI, you may find this useful

https://github.com/egeniq/BetterSheet

It has the feature you want.

---Edited---

Revisit the problem after months. If you don't want extra library, https://stackoverflow.com/a/61239704/10800901 would serve you well. Very elegant solution. Work like a charm.

Junyi Wang
  • 154
  • 1
  • 6