villb
villb

Reputation: 311

Is it possible to use swipe to dismiss while presenting a fullscreen modal in iOS 13?

With the new default card modal presentation in iOS 13, there is a nice feature that is swipe to dismiss. Is it possible to use this feature while presenting a classic .fullscreen modal?

I checked and if isModalInPresentation is false while presenting in fullscreen.

Any ideas?

Upvotes: 5

Views: 4833

Answers (3)

Prathap Ms
Prathap Ms

Reputation: 277

This works for me:

struct SwipeFullScreenCover: View {

    @GestureState private var dragOffset = CGSize.zero
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        ZStack{ .... }
            .gesture(DragGesture().updating($dragOffset, body: { (value, state, transaction) in
                if(value.startLocation.x < 20 &&
                   value.translation.width > 100) {
                    self.presentationMode.wrappedValue.dismiss()
                }
            }))
    }
}

Upvotes: 2

AndyDeveloper
AndyDeveloper

Reputation: 3030

As @pepsy said, the full screen view is not intended to be dismissed by swiping. However, if you still want to try it, here's something I started

NOTE: it's a bit glitchy the parent controller is not visible while dragging.

I added a UIPanGestureRecognizer to the view to simulate the swipe to dismiss behavior. I also have a Close (X) button, as the swipe gesture is not as intuitive as for a sheet.

@IBAction func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {

    let translationY = sender.translation(in: sender.view!).y

    switch sender.state {
    case .began:
        break
    case .changed:
        view.transform = CGAffineTransform(translationX: 0, y: translationY)
    case .ended, .cancelled:
        if translationY > 160 {
            dismiss(animated: true, completion: nil)
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.view.transform = CGAffineTransform(translationX: 0, y: 0)
            })
        }
    case .failed, .possible:
        break
    @unknown default:
        break
    }
}

Upvotes: 2

pepsy
pepsy

Reputation: 1457

It seems that the swipe to dismiss will only work if the modal is presented as a sheet, as stated in this year's wwdc:

Now, what do you all have to do to support Pull to Dismiss? In general, nothing. If you present something as a Sheet, the ability to pull it down comes for free.

And it makes sense. When you present it as a sheet, the UI makes it look like you can swipe the modal down. When you present it on fullscreen, it would not be intuitive for the user that he should swipe the page down to dismiss. I'd rather use a button on this case.

Upvotes: 3

Related Questions