【问题标题】:How to prevent a modal UIImagePickerController from rotating?如何防止模态 UIImagePickerController 旋转?
【发布时间】:2011-05-20 09:55:37
【问题描述】:

我有一个完全支持旋转的应用程序。我正在模态地添加一个UIImagePickerController,它不支持UIInterfaceOrientationLandscape,我无法让控制器保持纵向。

换句话说,我需要禁用 UIImagePickerController 的旋转,以便它保持纵向,而不删除我应用程序其余部分的旋转。这似乎很基本,但我似乎无法找到它。如何防止这种轮换?



更新

按照建议,我尝试使用以下代码进行子类化:

@interface UAImagePickerController : UIImagePickerController {
}
@end

@implementation UAImagePickerController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIDeviceOrientationIsPortrait(toInterfaceOrientation);
}
@end

这条线根本没有被断点击中……我认为UIImagePickerView一定有什么奇怪的地方

【问题讨论】:

  • 作为记录,我只是注意到我的 iPhone 应用程序中的 imagepickerview 会旋转,即使该应用程序不支持横向。所以似乎没有“合法”的方式来禁用相机视图的旋转?听起来像是雷达,或者至少是一个变更请求。

标签: iphone ios uiviewcontroller rotation uiimagepickercontroller


【解决方案1】:

我在 UIViewController 中创建了我的 UIImagePickerController,并实现了相应的 shouldAutorotateToInterfaceOrientation 方法并像这样呈现它

[self.view addSubview:myImagePicker.view];
[targetVC.navigationController presentModalViewController:self animated:YES];

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    子类 UIImagePickerController 并让新类实现 shouldAutorotateToInterfaceOrientation: 使其仅旋转为纵向,如下所示:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    

    【讨论】:

    • 作为您答案的补充,您应该始终(现在)使用 UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait 宏。例如,不支持至少 2 个方向(即纵向向上和纵向向下或横向左侧和横向右侧)的 iPad 应用程序将被拒绝。这些宏在横向或纵向返回 YES,具体取决于您使用的方式。
    • @jer 我已经提交了一个 iPad 应用程序,它只支持 1 个方向并且没有被拒绝,所以我不确定你从哪里得出这个断言。至于代码本身,它是 Xcode 在您创建新的视图控制器类时自动生成的,所以如果您确定它容易被拒绝,您应该向 Apple 提交错误。
    • HIG 表示应用程序必须至少支持 2 个方向。然而,它确实说某些应用程序(游戏)可能无法支持多个方向。但是,有些游戏因不支持多方向而被拒绝。因此,当您只支持一条路时,您就可以耕种自己的道路。
    • 无论如何,它不会像普通子类那样被触发。 Apple 说 UIImagePickerController 只支持肖像,所以你的论点在这里没有实际意义。
    • 我正在使用一些特殊的嵌入式导航控制器,并且在正确链接它们时遇到了一些问题。我现在可以阻止它旋转,但我仍然试图让它一直呈现肖像,即使它的父级是风景。只是这个变化无常的 UIImagePickerController 很麻烦。
    【解决方案3】:

    您应该覆盖 _isSupportedInterfaceOrientation:,而不是 shouldAutorotateToInterfaceOrientation:

    iPad 2 UIImagePickerController camera auto-rotation driving me mad!

    【讨论】:

    • 这是一种私有方法,Apple 不支持。这不是推荐的解决方案,您可能会因为使用它而被拒绝。
    • 谢谢帽子!!!但这是工作。是否会被拒绝取决于您的实施。有一种方法可以混淆任何会检查它的人;)
    【解决方案4】:

    只需将其放在 ViewControllers @implementation 块上方(在 .m 文件中)

    @implementation UIImagePickerController (portrait)
    
    - (BOOL)_isSupportedInterfaceOrientation:(UIDeviceOrientation)orientation
    {
        return UIDeviceOrientationIsPortrait(orientation);
    }
    
    @end
    

    【讨论】:

    • 私有方法违反苹果开发者协议,具有危险性,不应置于任何生产环境中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    相关资源
    最近更新 更多