禁用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:52:55  来源:igfitidea点击:

Disable the interactive dismissal of presented view controller in iOS 13

iosuiviewcontrolleruikitmodalviewcontrollerios13

提问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)......

The new sliding modal presentation in iOS 13

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.

…我们可以通过向下滑动呈现的视图控制器(交互式关闭)来关闭这些工作表。尽管新的“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 dismissal

残疾人互动解雇

(Disabled interactive .pageSheetdismissal acts like this.)

(禁用交互式.pageSheet解雇行为像这样。)

  • Since the iOS 13, UIViewControllercontains a new property called isModalInPresentationwhich must be set to trueto 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.
  • 从 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, UIAdaptivePresentationControllerDelegatecontains a new method called presentationControllerShouldDismiss.
  • This method is called only if the presented view controller is not dismissed programmatically and its isModalInPresentationproperty is set to false.
  • 从 iOS 13 开始,UIAdaptivePresentationControllerDelegate包含一个名为presentationControllerShouldDismiss.
  • 仅当呈现的视图控制器未以编程方式解除并且其isModalInPresentation属性设置为 时才会调用此方法false

Tip:Don't forget to assign presentationController's delegate.

提示:不要忘记分配presentationController 的委托。

回答by Bilal

  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 Screenform the Presentationdropdown.

    enter image description here

  2. If you just want to disable the interactive dismissal and keep the new presentation style set UIViewControllerproperty isModalInPresentationto true.

    if #available(iOS 13.0, *) {
        someViewController.isModalInPresentation = true // available in IOS13
    }
    
  1. 如果您想要与以前的 iOS 版本(< iOS13)相同的行为,如全屏模型展示,只需将目标视图控制器的展示样式设置为 UIModalPresentationStyle.fullScreen

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

    如果你使用的是故事板只需选择segua并选择Full Screen形成Presentation下拉。

    在此处输入图片说明

  2. 如果您只想禁用交互式关闭并将新的演示文稿样式设置UIViewController属性保留isModalInPresentationtrue.

    if #available(iOS 13.0, *) {
        someViewController.isModalInPresentation = true // available in IOS13
    }
    

回答by Mohit Tomar

The property isModalInPresentationmight 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 isModalInPresentationas many users suggestion.

它使用isModalInPresentation尽可能多的用户建议。