首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDocumentPickerViewController.ModalPresentationStyle不能在iOS 16.1中更改

UIDocumentPickerViewController.ModalPresentationStyle不能在iOS 16.1中更改
EN

Stack Overflow用户
提问于 2022-10-31 16:23:32
回答 1查看 86关注 0票数 0

我有一个依赖于Xamarin.IOS的应用程序,它可以在某个时候将文档选择器显示为弹出器。在更新到XCode14并为iOS16做了一个构建之后,我发现文档选择器显示错误(在FormSheet样式中,而不是在Popover风格中)。

造成这种情况的原因似乎是,试图更改ModalPresentationStyle的尝试正在悄然失败,并且保持为相同的默认值-- FormSheet。

在一个简单的测试应用程序中,用一个简单的按钮点击处理程序将它复制到应用程序的外部。在这里,我希望ModalPresentationStyle会改变,或者至少抛出某种错误,如果不支持的话。相反,它静默地保持为UIModalPresentationStyle.FormSheet。

代码语言:javascript
复制
partial void BtnClick(UIKit.UIButton sender)
{
    var allowedUtis = new List<string>() { ".txt" };
    var documentPicker = new UIDocumentPickerViewController(
        allowedUtis.ToArray(), 
        UIDocumentPickerMode.Import);

    var previousValue = documentPicker.ModalPresentationStyle;
    documentPicker.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    Debug.WriteLine($"Changed from {previousValue} to {documentPicker.ModalPresentationStyle}");

    if (documentPicker.PopoverPresentationController != null)
    {
        documentPicker.PopoverPresentationController.SourceView = sender;
        documentPicker.PopoverPresentationController.SourceRect = sender.Bounds;
        documentPicker.PopoverPresentationController.PermittedArrowDirections = UIPopoverArrowDirection.Up;
    }

    PresentModalViewController(documentPicker, true);
}

同时,在一个测试应用程序中复制了同样的行为,以检验问题不是Xamarin.IOS。在这里,modalPresentationStyle的值仍然是.formSheet (2)。

代码语言:javascript
复制
let supportedTypes: [UTType] = [UTType.audio]
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) 1017

print(String(describing: pickerViewController.modalPresentationStyle));
pickerViewController.modalPresentationStyle = .popover
print(String(describing: pickerViewController.modalPresentationStyle));

self.present(pickerViewController, animated: true, completion: {})

这不是在XCode13上发生的,而是在XCode14.01上在运行iOS 16.1的第8代iPad上发生的。

使用运行iOS 16.0的模拟器无法在XCode14.01上复制。

预期的行为改变了吗?我似乎在这方面的文档发布说明中找不到任何东西。

EN

回答 1

Stack Overflow用户

发布于 2022-10-31 16:59:49

我最近也遇到了同样的基本问题。解决方案是在您呈现模态视图控制器之后设置popoverPresentationController

代码语言:javascript
复制
let supportedTypes: [UTType] = [UTType.audio]
let pickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) 1017

print(String(describing: pickerViewController.modalPresentationStyle));
pickerViewController.modalPresentationStyle = .popover
print(String(describing: pickerViewController.modalPresentationStyle));

self.present(pickerViewController, animated: true)

pickerViewController.popoverPresentationController?.sourceRect = sender.bounds
pickerViewController.popoverPresentationController?.sourceView = sender
pickerViewController.popoverPresentationController?.permittedArrowDirections = .up

在调用popoverPresentationController之前,视图控制器的nilpresent。至少在iOS 16下是这样的。

虽然我没有在UIDocumentPickerViewController中专门测试这一点,但是这个解决方案适用于我试图呈现的通用模态视图控制器。如果popoverPresentationController是在调用present之前而不是以后设置的,则视图控制器将显示为窗体工作表,而不是设置为弹出器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74266286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档