简体   繁体   English

在 iPad 上展示视图控制器只允许 .Popover

[英]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.我什至尝试设置sourceViewsourceRect ,这确实会阻止错误,但是,它将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 ,您必须按照错误状态进行操作并设置sourceViewsourceRectbarButtonItem适当组合。

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.

相关问题 Popover视图控制器在iPad上崩溃 - Popover view controller crashes on iPad 仅在UISplitViewController的DetailViewController中呈现视图控制器 - Presenting a View controller only in the DetailViewController of UISplitViewController 如何在视图外部点击iPad上关闭popover View控制器:IOS 9 - How to dismiss popover View controller on iPad on tapping outside the view : IOS 9 使用UISearchDisplayController时,弹出窗口不会显示在iPad视图控制器上 - When using UISearchDisplayController the popover does not show on an iPad view controller 如何设置已经在iPad中显示的Popover View Controller框架? - How to set popover view controller frame while it is already displayed in iPad? 在iPad上的popover中推送导航视图控制器时动画popoverContentsize - Animate popoverContentsize when pushing navigation view controller in popover on iPad 在iPad上弹出导航视图控制器时更改弹出控制器的大小 - changing the size of the popover controller when pushing navigation view controller in popover on iPad iPad / iOS7:从中呈现“全屏”视图控制器后,“页面”模式视图控制器出现异常行为 - iPad/iOS7: 'Page' modal view controller strange behaviour after presenting 'Full screen' view controller from it 呈现视图控制器时崩溃 - Crash in presenting a view controller 从可重用视图内部显示弹出视图? - Presenting popover view from inside reusable view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM