[英]Presenting View Controller on iPad only allows .Popover
I have an iPhone application that I am trying to make universal.我有一个 iPhone 应用程序,我正在尝试使其通用。 I have this code:
我有这个代码:
let documentMenu = UIDocumentMenuViewController(documentTypes: [kUTTypeContent as String], inMode: .Import)
documentMenu.modalPresentationStyle = .FormSheet
documentMenu.delegate = self
self.presentationContext.presentViewController(documentMenu, animated: true, completion: nil)
self.presentationContext
is just a view controller passed into the class. self.presentationContext
只是传递给类的视图控制器。
Every time this code is executed, this error occurs:每次执行此代码时,都会出现此错误:
Your application has presented a UIDocumentMenuViewController ().
您的应用程序呈现了一个 UIDocumentMenuViewController()。 In its current trait environment, the modalPresentationStyle of a UIDocumentMenuViewController with this style is UIModalPresentationPopover.
在其当前的 trait 环境中,具有此样式的 UIDocumentMenuViewController 的 modalPresentationStyle 是 UIModalPresentationPopover。 You must provide location information for this popover through the view controller's popoverPresentationController.
您必须通过视图控制器的 popoverPresentationController 提供此弹出框的位置信息。 You must provide either a sourceView and sourceRect or a barButtonItem.
您必须提供 sourceView 和 sourceRect 或 barButtonItem。 If this information is not known when you present the view controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.
如果您在展示视图控制器时不知道此信息,您可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。
I am not sure what's going on.我不确定发生了什么。 I have even tried to set the
sourceView
and sourceRect
, to which does stop the error, however, it sticks the DocumentMenuViewController
into a popover, I do not what that.我什至尝试设置
sourceView
和sourceRect
,这确实会阻止错误,但是,它将DocumentMenuViewController
sourceRect
到弹出窗口中,我不这样做。 I need this modally presented in the center of the screen.我需要在屏幕中央以模态呈现。 Any help is appreciated.
任何帮助表示赞赏。
Following code is working fine以下代码工作正常
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
The issue is that UIDocumentMenuViewController
doesn't want to be displayed as anything other than a smaller, "popover" presentation style.问题是
UIDocumentMenuViewController
不希望显示为较小的“popover”演示样式以外的任何内容。 Its own implementation overrides any setting of its modalPresentationStyle
to .Popover
.它自己的实现将其
modalPresentationStyle
任何设置覆盖为.Popover
。 So your attempt to set the style to .FormSheet
is ultimately ignored.因此,您将样式设置为
.FormSheet
尝试最终会被忽略。
This is what causes the error.这就是导致错误的原因。 Once the style is
.Popover
, you must do what the error states and set some appropriate combination of sourceView
and sourceRect
or barButtonItem
.一旦样式为
.Popover
,您必须按照错误状态进行操作并设置sourceView
和sourceRect
或barButtonItem
适当组合。
File an enhancement request with Apple to allow support for other modal presentation styles.向 Apple 提交增强请求,以允许支持其他模态演示样式。 In the meantime, you need to adjust your UI.
同时,您需要调整您的用户界面。
let documentMenu = UIDocumentMenuViewController(documentTypes: [kUTTypeContent as String], inMode: .Import)
documentMenu.popoverPresentationController?.sourceView = self // UIView
documentMenu.modalPresentationStyle = .popover
documentMenu.delegate = self
self.presentationContext.presentViewController(documentMenu, animated: true, completion: nil)
my two cents for Mac Catalyst:我的 Mac Catalyst 两分钱:
on Mac Catalyst you must use .formSheet在 Mac Catalyst 上,您必须使用 .formSheet
so:所以:
#if targetEnvironment(macCatalyst)
let modalPresentationStyle : UIModalPresentationStyle = .formSheet
#else
let modalPresentationStyle : UIModalPresentationStyle = .popover
#endif
optionsVC.modalPresentationStyle = modalPresentationStyle
optionsVC.popoverPresentationController?.sourceView = self.view
self.present(optionsVC, animated: true) {
}
eventually add:最后补充:
optionsVC.modalPresentationStyle = modalPresentationStyle
to prevent tapping outside will close without invoking callbacks (iOs13 / Catalyst only)防止敲击外部将关闭而不调用回调(仅限 iOs13 / Catalyst)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.