我有一个依赖于Xamarin.IOS的应用程序,它可以在某个时候将文档选择器显示为弹出器。在更新到XCode14并为iOS16做了一个构建之后,我发现文档选择器显示错误(在FormSheet样式中,而不是在Popover风格中)。
造成这种情况的原因似乎是,试图更改ModalPresentationStyle的尝试正在悄然失败,并且保持为相同的默认值-- FormSheet。
在一个简单的测试应用程序中,用一个简单的按钮点击处理程序将它复制到应用程序的外部。在这里,我希望ModalPresentationStyle会改变,或者至少抛出某种错误,如果不支持的话。相反,它静默地保持为UIModalPresentationStyle.FormSheet。
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)。
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上复制。
预期的行为改变了吗?我似乎在这方面的文档发布说明中找不到任何东西。
发布于 2022-10-31 16:59:49
我最近也遇到了同样的基本问题。解决方案是在您呈现模态视图控制器之后设置popoverPresentationController。
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之前,视图控制器的nil是present。至少在iOS 16下是这样的。
虽然我没有在UIDocumentPickerViewController中专门测试这一点,但是这个解决方案适用于我试图呈现的通用模态视图控制器。如果popoverPresentationController是在调用present之前而不是以后设置的,则视图控制器将显示为窗体工作表,而不是设置为弹出器。
https://stackoverflow.com/questions/74266286
复制相似问题