Disable the interactive dismissal of presented view controller
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 thePresentation
dropdown.If you just want to disable the interactive dismissal and keep the new presentation style set
UIViewController
propertyisModalInPresentation
totrue
.if #available(iOS 13.0, *) { someViewController.isModalInPresentation = true // available in IOS13 }
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
}
}
Option 1:
viewController.isModalInPresentation = true
(Disabled interactive .pageSheet
dismissal acts like this.)
- Since the iOS 13,
UIViewController
contains a new property calledisModalInPresentation
which must be set totrue
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 calledpresentationControllerShouldDismiss
. - This method is called only if the presented view controller is not dismissed programmatically and its
isModalInPresentation
property is set tofalse
.
Tip: Don't forget to assign
presentationController
's delegate. But be aware, it is known that even just accessing thepresentationController
can cause a memory leak.
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)