问题
iOS 13 introduces a new design of modalPresentationStyle .pageSheet (and its sibling .formSheet) for modally presented view controllers…
…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.
回答1:
Option 1:
viewController.isModalInPresentation = true
(Disabled interactive .pageSheet dismissal acts like this.)
- Since the iOS 13,
UIViewControllercontains a new property calledisModalInPresentationwhich must be set totrueto 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
.popoveretc. - This property is
falseby 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,
UIAdaptivePresentationControllerDelegatecontains a new method calledpresentationControllerShouldDismiss. - This method is called only if the presented view controller is not dismissed programmatically and its
isModalInPresentationproperty is set tofalse.
Tip: Don't forget to assign presentationController's delegate.
回答2:
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.fullScreenlet someViewController = \*VIEW CONTROLLER*\ someViewController.modalPresentationStyle = .fullScreenAnd if you are using storyboard just select the segua and select
Full Screenform thePresentationdropdown.If you just want to disable the interactive dismissal and keep the new presentation style set
UIViewControllerpropertyisModalInPresentationtotrue.if #available(iOS 13.0, *) { someViewController.isModalInPresentation = true // available in IOS13 }
回答3:
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
回答4:
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)
来源:https://stackoverflow.com/questions/56459329/disable-the-interactive-dismissal-of-presented-view-controller-in-ios-13