禁用 iOS 13 中呈现的视图控制器的交互式关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56459329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable the interactive dismissal of presented view controller in iOS 13
提问by Jakub Truhlá?
iOS 13introduces a new design of modalPresentationStyle
.pageSheet
(and its sibling .formSheet
) for modally presented view controllers…
iOS 13为模态呈现的视图控制器引入了一种新设计modalPresentationStyle
.pageSheet
(及其同级.formSheet
)......
…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.
…我们可以通过向下滑动呈现的视图控制器(交互式关闭)来关闭这些工作表。尽管新的“pull-to-dismiss”功能非常有用,但它可能并不总是可取的。
THE QUESTION:How can we turn the interactive dismissal off? - Bear in mind we keep the presentation style the same.
问题:我们如何关闭交互式解雇? - 请记住,我们保持演示风格相同。
回答by Jakub Truhlá?
Option 1:
选项1:
viewController.isModalInPresentation = true
(Disabled interactive .pageSheet
dismissal acts like this.)
(禁用交互式.pageSheet
解雇行为像这样。)
- 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.
- 从 iOS 13 开始,
UIViewController
包含一个名为的新属性isModalInPresentation
,必须将其设置true
为防止交互式关闭。 - 它基本上忽略了视图控制器边界之外的事件。请记住,如果您不仅使用自动样式,还使用演示样式等
.popover
。 - 该属性是
false
默认的。
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.
来自官方文档: If
true
,UIKit 会忽略视图控制器边界之外的事件,并防止视图控制器在屏幕上时交互式关闭。
Option 2:
选项 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
.
- 从 iOS 13 开始,
UIAdaptivePresentationControllerDelegate
包含一个名为presentationControllerShouldDismiss
. - 仅当呈现的视图控制器未以编程方式解除并且其
isModalInPresentation
属性设置为 时才会调用此方法false
。
Tip:Don't forget to assign presentationController's delegate.
提示:不要忘记分配presentationController 的委托。
回答by Bilal
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 }
如果您想要与以前的 iOS 版本(< iOS13)相同的行为,如全屏模型展示,只需将目标视图控制器的展示样式设置为
UIModalPresentationStyle.fullScreen
let someViewController = \*VIEW CONTROLLER*\ someViewController.modalPresentationStyle = .fullScreen
如果你使用的是故事板只需选择segua并选择
Full Screen
形成Presentation
下拉。如果您只想禁用交互式关闭并将新的演示文稿样式设置
UIViewController
属性保留isModalInPresentation
为true
.if #available(iOS 13.0, *) { someViewController.isModalInPresentation = true // available in IOS13 }
回答by Mohit Tomar
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.
当您将其设置为 时
true
,UIKit 会忽略视图控制器边界之外的事件,并防止视图控制器在屏幕上时交互式关闭。
You can use it like this:
你可以这样使用它:
let controller = MyViewController()
controller.isModalInPresentation = true
self.present(controller, animated: true, completion: nil)
回答by Brandon Whitton
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
回答by Arda O?ul ü?p?nar
Apple shared a sample code about it at this link
Apple在此链接上分享了有关它的示例代码
It uses isModalInPresentation
as many users suggestion.
它使用isModalInPresentation
尽可能多的用户建议。