在iPad上呈现视图控制器只允许.Popover。

3

我有一个 iPhone 应用程序,我正在尝试将其制作为通用应用。我有以下代码:

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 是传递给类的视图控制器。

每次执行此代码时,都会出现以下错误:

您的应用程序已显示了一个 UIDocumentMenuViewController ()。在其当前的特征环境中,具有此样式的 UIDocumentMenuViewController 的 modalPresentationStyle 为 UIModalPresentationPopover。您必须通过视图控制器的 popoverPresentationController 提供此弹出窗口的位置信息。您必须提供 sourceView 和 sourceRect 或 barButtonItem 中的任一项。如果在呈现视图控制器时不知道此信息,可以在 UIPopoverPresentationControllerDelegate 方法 -prepareForPopoverPresentation 中提供它。

我不确定发生了什么。我甚至尝试设置 sourceViewsourceRect,这确实停止了错误,但是它将 DocumentMenuViewController 粘贴到了弹出窗口中,我不需要那样做。我需要以模态方式在屏幕中央呈现此视图控制器。谢谢您的帮助。

4个回答

4
以下代码运行良好。
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)

2
问题在于UIDocumentMenuViewController不想以除了更小的"popover"展示风格以外的任何方式显示。它自己的实现会覆盖其modalPresentationStyle设置为.Popover。因此,您尝试将样式设置为.FormSheet最终会被忽略。
这就是引起错误的原因。一旦样式为.Popover,您必须按照错误指示设置一些适当的sourceViewsourceRectbarButtonItem的组合。
向苹果提交改进建议以允许支持其他模态呈现样式。同时,您需要调整您的UI。

我并不是在暗示你错了,但是有没有任何文档来支持你的说法呢? - crizzwald
很遗憾,我的答案基于对UIDocumentMenuViewController的实际经验,而你在问题中提到的经验与我所见相符。 - rmaddy

0
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)

0

关于 Mac Catalyst,我的建议是:

在 Mac Catalyst 上,您必须使用 .formSheet。

因此:

   #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) {
    }

最终添加:
optionsVC.modalPresentationStyle = modalPresentationStyle

为了防止在iOs13 / Catalyst上点击外部区域而不调用回调函数而关闭


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接