Option 1:
viewController.isModalInPresentation = true
(Disabled interactive .pageSheet
dismissal acts like this.)
(禁用的交互式.pageSheet取消如下所示。)
- 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.
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.
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
}
The property isModalInPresentation
might help.
属性isModalInPresentation可能会有所帮助。
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)
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:
在ViewDidLoad上,如果您的ViewController已在导航控制器中显示:
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
}
}
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.
如果您正在使用情节提要来布局您的UI,我已经找到了在使用导航控制器时禁用此交互取消的最佳方法是将属性检查器中导航控制器的显示从自动更改为全屏。然后,您的导航堆栈中的所有视图控制器都将是全屏的,并且不能被用户取消。
Attribute Inspector showing presentation option for the navigation controller
显示导航控制器显示选项的属性检查器
Apple shared a sample code about it at this link
苹果在这个链接上分享了一个关于它的示例代码
It uses isModalInPresentation
as many users suggestion.
它使用ismodalInPresentation作为许多用户的建议。
i was try to so hard for solve this problem but the solution are work for me is i select ViewController in **mainstoryboard > inspector > presentation > Full Screen ** default Automatic to change FullScreen
我一直很努力地试图解决这个问题,但我的解决方案是在**主故事板>检查器>演示>全屏**默认自动更改全屏中选择查看控制器
it's work for me
这是我的工作
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
更多回答
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: medium.com/@hacknicity/…
如果呈现的视图控制器是导航控制器,则可以在导航控制器上或在导航堆栈中显示的各个视图控制器上设置isModalInPresentation。后者允许您逐个屏幕地选择是否可以进行交互式解雇。小心使用搜索控制器,它们优先于单独的视图控制器(但不是导航控制器)。在我的博客帖子中有更多信息:Medium.com/@hacknicity/…
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
请记住,如果您的VC显示为弹出窗口,这将防止在弹出窗口之外点击时取消弹出窗口
Objective-C: viewController.modalInPresentation = YES;
Objective-C:viewController.modalInPresentation=yes;
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.
在UIImagePickerController上设置此标志时,有人让它工作吗?对我们来说,UIImagePickerController似乎忽略了这一点,所以可以通过滑动手势来关闭它。也许这是iOS 13的一个漏洞。
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.
当PresentationControllerShouldDismi(_PresentationController:UIPresentationController)返回FALSE时,将调用PresentationControllerDidAttemptToDismi(_PresentationController:UIPresentationController),以便您可以在用户输入数据后执行添加确认对话框之类的操作。
It works better than using {controller.isModalInPresentation = true }.
它比使用{Controler.isModalInPresentation=true}工作得更好。
Does second approach work with previous iOS version? < iOS13.0
第二种方法是否适用于以前的iOS版本?
Please do not post duplicate answers.
请不要发布重复的答案。
我是一名优秀的程序员,十分优秀!