ios - iOS 11 UIImagePickerController 奇怪的问题
问题描述
我正在使用 UIImagePickerController 从照片库中选择单个图像。iPad处于横向模式时有一个奇怪的问题。
推荐使用 iPad 上的 UIPopoverPresentationController 呈现图像选择器。首次出现时,状态栏是正确的:
但是,当进入第二层照片库时,状态栏变成了竖屏模式:
到目前为止,我注意到的是:
- 此问题仅出现在 iOS 11 中,而不出现在 iOS 10 中。
- 发生这种情况时,将 iPad 旋转为纵向并返回横向将修复状态栏方向。
- 它只发生在第一次展示选择器控制器时。
- 如果忽略,其他模态视图将以纵向模式呈现:
呈现 uiimagepickerController 的代码如下:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.modalPresentationStyle = UIModalPresentationPopover;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
UIPopoverPresentationController *popupController = picker.popoverPresentationController;
if (popupController) {
popupController.barButtonItem = sender;
}
知道我做错了什么,或者这是一个错误吗?
整个示例项目可以在这里下载: https ://www.dropbox.com/s/zgipclyr0mz26c6/test.zip?dl=0
解决方案
我终于找到了我的问题的原因。
我的应用需要支持 iPad 上的所有方向和 iPhone 上的人像模式。因此我添加了以下 UIApplicationDelegate 代码:
- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (window.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
但有时它会给我 nil 窗口,例如在 iPad 上使用 UIPopoverPresentationController 呈现的 UIImagePickerController 的情况,并将返回 UIInterfaceOrientationMaskPortrait 并导致状态栏旋转到纵向模式。我还注意到只有在选中 UIRequiresFullScreen 时才会发生这种情况。
我通过检查窗口不为零解决了我的问题,如下所示:
- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (window) {
if (window.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait;
}
} else {
return UIInterfaceOrientationMaskAll;
}
}
推荐阅读
- java - 为多种 xsd 类型指定一个适配器
- reactjs - 如何重新加载 localStorage 保存的 aws-amplify 身份验证?
- amazon-redshift - Redshift Query Scheduler 状态为:已启用,但查询未运行。IAM 权限问题?
- blazor - 是否添加可选@?
- google-cloud-platform - BigQuery 视图在 SELECT * 查询中显示正确结果,但在推送到表时显示正确结果
- android - 错误:NG0201:在 NodeInjector 中找不到 NgControl 的提供程序
- jenkins - config.ini 秘密文件中的特殊字符 - jenkins
- r - 替换 R 中的 NA
- jquery - 为 CloudTables DataTable 实现导出 CSV 文件按钮的困惑
- discord.js - JavaScript discord bot TypeError:无法读取未定义的属性“公会”