2

I’m creating an app where I have create first screen (it will be short description of application) and on the screen I have a next Button if I click on next button it should be dismiss otherwise it must not be dismiss either pull down.

If user pull down a sheet, it should be again re-position.

The problem is, that the user can dismiss the modal by swiping it down and application dashboard screen show that should be prevented.

How can we prevent to dismiss the Model by pull down.

struct ModalView : View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {

        Rectangle()
            .fill(Color.orange)
            .frame(width: 400, height: 650)
            .overlay(
                VStack{
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }) {
                        HStack {
                            Image(systemName: "chevron.left")
                            Text("Dismiss")
                        }.padding(10.0)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10.0)
                                .stroke(lineWidth: 2.0)
                        )
                    }.accentColor(.white)
                })
            .border(Color.blue)
            .gesture( DragGesture())
    }
}

ContentView

struct ContentView: View {

    //MARK: Properties
    //isPresented:- Present's a Welcome Screen in the form of cards.
    @State private var isPresented = true
    var body: some View {

        VStack{
            DashboardView()
                .sheet(isPresented: $isPresented){
                    //IntroductionView(isPresentingSheet: self.$isPresented)
                    ModalView()
            }
        }
    }
}

DashboardView

struct DashboardView: View {

    var body: some View {
        Text("Hello SwiftUI")
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ravindra_Bhati
  • 1,071
  • 13
  • 28

1 Answers1

1

You can try this solution:

struct ModalWrapper: View {

    var body: some View {
        ModalView().highPriorityGesture(DragGesture())
    }

}

struct ModalView : View {

    @Environment(\.presentationMode) var presentationMode

    var body: some View {

        Rectangle()
            .fill(Color.orange)
            .frame(width: 400, height: 650)
            .overlay(
                VStack{
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }) {
                        HStack {
                            Image(systemName: "chevron.left")
                            Text("Dismiss")
                        }.padding(10.0)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10.0)
                                .stroke(lineWidth: 2.0)
                        )
                    }.accentColor(.white)
                })
            .border(Color.blue)
            .highPriorityGesture(DragGesture())
    }
}

struct ContentView: View {

    //MARK: Properties
    //isPresented:- Present's a Welcome Screen in the form of cards.
    @State private var isPresented = true
    var body: some View {

        VStack{
            DashboardView()
                .sheet(isPresented: $isPresented){
                    //IntroductionView(isPresentingSheet: self.$isPresented)
                    ModalWrapper()
            }
        }
    }
}

struct DashboardView: View {

    var body: some View {
        Text("Hello SwiftUI")
    }

}

Here I have added ModalWrapper for wrap the modal view Or else you will have to add highPriorityGesture(DragGesture()) to all subviews of the ModalView So it is better to keep one wrapper view.

Hope this will help you.

kchopda
  • 312
  • 4
  • 16
  • when i pull down or (Drag Down ModalView, it's getting closed. it should not be closed. ModelView screen should close when i click on the dismiss Button Only. – Ravindra_Bhati Feb 13 '20 at 09:17
  • Please try this solution it is working for me. I am using Xcode 11.3.1 and tested in iOS13 and lator. – kchopda Feb 14 '20 at 06:59