首页 > 解决方案 > iOS 11 UIImagePickerController 奇怪的问题

问题描述

我正在使用 UIImagePickerController 从照片库中选择单个图像。iPad处于横向模式时有一个奇怪的问题。

推荐使用 iPad 上的 UIPopoverPresentationController 呈现图像选择器。首次出现时,状态栏是正确的:

在此处输入图像描述

但是,当进入第二层照片库时,状态栏变成了竖屏模式:

在此处输入图像描述

到目前为止,我注意到的是:

  1. 此问题仅出现在 iOS 11 中,而不出现在 iOS 10 中。
  2. 发生这种情况时,将 iPad 旋转为纵向并返回横向将修复状态栏方向。
  3. 它只发生在第一次展示选择器控制器时。
  4. 如果忽略,其他模态视图将以纵向模式呈现:

在此处输入图像描述

呈现 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

标签: iosobjective-cuiimagepickercontroller

解决方案


我终于找到了我的问题的原因。

我的应用需要支持 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;
    }
 }

推荐阅读