I've presented a modal view but I would like the user to go through some steps before it can be dismissed. Currently the view can be dragged to dismiss.
Is there a way to stop this from being possible?
I've watched the WWDC Session videos and they mention it but I can't seem to put my finger on the exact code I'd need.
struct OnboardingView2 : View { @Binding var dismissFlag: Bool var body: some View { VStack { Text("Onboarding here! 🙌🏼") Button(action: { self.dismissFlag.toggle() }) { Text("Dismiss") } } } }
I currently have some text and a button I'm going to use at a later date to dismiss the view.
Starting from iOS 15 we can use interactiveDismissDisabled
:
func interactiveDismissDisabled(_ isDisabled: Bool = true) -> some View
We just need to attach it to the sheet. Here is an example from the documentation:
struct PresentingView: View { @Binding var showTerms: Bool var body: some View { AppContents() .sheet(isPresented: $showTerms) { Sheet() } } } struct Sheet: View { @State private var acceptedTerms = false var body: some View { Form { Button("Accept Terms") { acceptedTerms = true } } .interactiveDismissDisabled(!acceptedTerms) } }
It is easy if you use the 3rd party lib Introspect, which is very useful as it access the corresponding UIKit component easily. In this case, the property in UIViewController
:
VStack { ... } .introspectViewController { $0.isModalInPresentation = true }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With