31

我正在创建一个处于横向模式的应用程序,我正在使用其中UIImagePickerController的 iPhone 相机拍照,我也想在横向模式下创建它。

但是正如 Apple 文档所暗示的那样UIImagePickerController,它不支持横向,那么我应该怎么做才能获得所需的功能呢?

4

9 回答 9

47

如果您想在横向模式下使用 UIImagePickerController,请使用user1673099 的答案,而不是:

- (BOOL)shouldAutorotate
{
    return NO;
}

利用:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

然后选择器将以横向模式打开:

在此处输入图像描述

但请确保您在部署信息中检查肖像:

在此处输入图像描述

于 2013-10-28T09:14:50.483 回答
34

...我也想在横向模式下创建它。

一行代码可以产生很大的不同!在您的 IBAction 所在的方法或函数中:

在斯威夫特,

let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// .overCurrentContext allows for landscape and portrait mode
imagePickerController.modalPresentationStyle = .overCurrentContext

目标-C,

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext];

注意:这将允许 imagePickerController 正确呈现它的视图,但可能无法解决 呈现时的旋转问题。

于 2017-02-28T17:59:07.643 回答
14

试试这个方法......

根据 Apple 文档,ImagePicker 控制器永远不会在横向模式下旋转。您只能在纵向模式下使用。

对于仅为 ImagePicker 控制器禁用横向模式,请遵循以下代码:

在您的 ViewController.m 中:

制作 Image Picker Controller 的 SubClass(NonRotatingUIImagePickerController)

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController
// Disable Landscape mode.
- (BOOL)shouldAutorotate
{
    return NO;
}
@end

使用如下

UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self; 
  etc.... Just as Default ImagePicker Controller

这对我有用,如果您有任何问题,请告诉我。

于 2013-10-15T06:09:04.413 回答
8

这适用于 iOS 10/11 中的Swift 4.0 。

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .all
    }
}

只需将扩展放在项目中的某处,无需子类化任何东西即可工作。

如果确实需要指定设备类型,可以添加如下检查:

import UIKit

extension UIImagePickerController {
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
    }
}

这将允许 iPad 自由旋转,但在手机上强制执行纵向模式。只需确保您的应用程序在其 info.plist 中配置为支持这些,否则您可能会在启动选择器时遇到崩溃。

于 2017-10-26T22:41:34.007 回答
6

这是一个支持所有界面方向旋转的版本:

/// Not fully supported by Apple, but works as of iOS 11.
class RotatableUIImagePickerController: UIImagePickerController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .all
  }
}

这样,如果用户旋转她的设备,它将更新选择器控制器以支持当前方向。只需像往常一样实例化 UIImagePickerController。

如果您只想支持方向的子集,则可以返回不同的值。

于 2017-10-26T20:09:05.823 回答
5

在没有任何技巧的情况下在横向模式下使用的正确方法UIImagePickerController是将其放入UIPopoverController

- (void)showPicker:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    _popover = [[UIPopoverController alloc] initWithContentViewController:picker];
    [_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
于 2014-07-28T11:48:08.850 回答
3

修改上面的代码方法

- (NSUInteger)supportedInterfaceOrientations
 {
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
     if(orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft)
         return UIInterfaceOrientationMaskLandscape;
     else
         return UIInterfaceOrientationMaskPortrait;
 }
于 2016-07-30T12:06:22.143 回答
2

接受的答案对我不起作用。我还必须将 modalPresentationStyle 添加到 UIImagePickerController 以使其正常工作。

UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
pickerController.delegate = self;
pickerController.allowsEditing = YES;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerController animated:YES completion:nil];

当然记得把它放在一个呈现选择器的控制器中:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape; //this will force landscape
}

但根据 Apple 的文档,不支持在横向模式下显示此选择器,因此请小心。

于 2016-12-02T08:30:14.003 回答
2

如果您正在寻找 SwiftUI 解决方案以及此处提到的内容,请查看此处。忽略 UIImagePickerController 可表示的安全区域解决了我的很多问题。

于 2021-10-05T17:38:26.147 回答