218

iOS 13 introduces a new design of modalPresentationStyle .pageSheet (and its sibling .formSheet) for modally presented view controllers…

The new sliding modal presentation in iOS 13

…and we can dismiss these sheets by sliding the presented view controller down (interactive dismissal). Although the new "pull-to-dismiss" feature is pretty useful, it may not always be desirable.

THE QUESTION: How can we turn the interactive dismissal off? - Bear in mind we keep the presentation style the same.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84

7 Answers7

422

Option 1:

viewController.isModalInPresentation = true

Disabled interactive dismissal

(Disabled interactive .pageSheet dismissal acts like this.)

  • Since the iOS 13, UIViewController contains a new property called isModalInPresentation which must be set to true to prevent the interactive dismissal.
  • It basically ignores events outside the view controller's bounds. Bear that in mind if you are using not only the automatic style but also presentation styles like .popover etc.
  • This property is false by default.

From the official docs: If true, UIKit ignores events outside the view controller's bounds and prevents the interactive dismissal of the view controller while it is onscreen.


Option 2:

func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
    return false
}
  • Since the iOS 13, UIAdaptivePresentationControllerDelegate contains a new method called presentationControllerShouldDismiss.
  • This method is called only if the presented view controller is not dismissed programmatically and its isModalInPresentation property is set to false.

Tip: Don't forget to assign presentationController's delegate. But be aware, it is known that even just accessing the presentationController can cause a memory leak.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
  • 5
    If the presented view controller is a navigation controller, you can either set `isModalInPresentation` on the navigation controller or on the individual view controllers shown in the navigation stack. The latter allows you to choose on a screen-by-screen basis whether the interactive dismissal is possible. Careful with search controllers, they take precedence over the individual view controller (but not the navigation controller). More info in my blog post: https://medium.com/@hacknicity/view-controller-presentation-changes-in-ios-13-ac8c901ebc4e – Geoff Hackworth Jun 08 '19 at 13:06
  • 2
    Keep in mind that if your VC is presented as a popover, this will prevent the popover from being dismissed when tapping outside of it – PatrickDotStar Sep 20 '19 at 12:36
  • 5
    Objective-C: `viewController.modalInPresentation = YES;` – quarac Oct 01 '19 at 15:35
  • 4
    Anyone got this to work when setting this flag on a `UIImagePickerController`? For us it looks like the `UIImagePickerController` is ignoring it and so it can be dismissed with the swipe gesture. Maybe it's an iOS 13 bug. – PatrickDotStar Oct 16 '19 at 12:18
  • 1
    https://developer.apple.com/documentation/uikit/view_controllers/disabling_pulling_down_a_sheet – raistlin Jan 27 '20 at 12:17
  • 4
    When `presentationControllerShouldDismiss(_ presentationController: UIPresentationController)` returns false, `presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController)` is called so you can do things like add a confirmation dialogue if the user has entered data. – Neil Feb 18 '20 at 19:45
  • I'm also seeing issues when using a `UIImagePickerController`, `presentationControllerShouldDismiss` is ignored but other delegate functions such as `presentationControllerWillDismiss` are called. – danfordham Mar 19 '20 at 15:44
90
  1. If you want the same behaviour as it's in previous iOS version (< iOS13) like model presentation in fullscreen, just set the presentation style of your destination view controller to UIModalPresentationStyle.fullScreen

    let someViewController = \*VIEW CONTROLLER*\
    someViewController.modalPresentationStyle = .fullScreen
    

    And if you are using storyboard just select the segua and select Full Screen form the Presentation dropdown.

    enter image description here

  2. If you just want to disable the interactive dismissal and keep the new presentation style set UIViewController property isModalInPresentation to true.

    if #available(iOS 13.0, *) {
        someViewController.isModalInPresentation = true // available in IOS13
    }
    
Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
Bilal
  • 18,478
  • 8
  • 57
  • 72
11

The property isModalInPresentation might help.

From the documentation:

When you set it to true, UIKit ignores events outside the view controller's bounds and prevents the interactive dismissal of the view controller while it is onscreen.

You can use it like this:

let controller = MyViewController()
controller.isModalInPresentation = true
self.present(controller, animated: true, completion: nil)
Theo
  • 3,826
  • 30
  • 59
Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40
10

If you have some business logic, something like all fields should be filled before dismissing, you should:

On ViewDidLoad if your ViewController has been presented within a Navigation Controller:

func viewDidLoad() { 
    self.navigationController?.presentationController?.delegate = self
}

If not, simply use

func viewDidLoad() { 
    self.presentationController?.delegate = self
}

Then implement the delegate method:

extension ViewController: UIAdaptivePresentationControllerDelegate {

    func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
        guard let text = firstName.text, text.isEmpty else { return false }
        guard let text = lastName.text, text.isEmpty else { return false }
        ...
    
        return true
    }

}
raed
  • 4,887
  • 4
  • 30
  • 49
5

If you are using storyboards to layout your UI I have found the best way to disable this interactive dismissal when using a navigation controller is to change the presentation of the Navigation Controller in the attribute inspector from Automatic to Full Screen. All view controllers in your navigation stack will then be full screen and will not be able to be dismissed by the user.

Attribute Inspector showing presentation option for the navigation controller

1

Apple shared a sample code about it at this link

It uses isModalInPresentation as many users suggestion.

Arda Oğul Üçpınar
  • 881
  • 1
  • 14
  • 38
-3

All solutions are good, but in my case, I need an option to stop movement. So this is a code for that.

if you want to block movement:

self.yourViewController?.presentedView?.gestureRecognizers?[0].isEnabled = false

And if you want to unblock movement:

self.yourViewController?.presentedView?.gestureRecognizers?[0].isEnabled = true
tBug
  • 789
  • 9
  • 13