在UIImagePickerController中强制横向方向

时间:2013-01-31 04:34:37

标签: ios objective-c xcode orientation uiimagepickercontroller

我是iOS开发的新手,我正在开发我的第一个应用程序。 (很抱歉,如果我问一个新手问题)

我的应用程序全部处于纵向,除了我正在使用 UIImagePickerController 来拍摄要在应用中使用的图片,我正在做的是:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
picker.allowsEditing = NO;
[self presentModalViewController:picker animated:YES];

由于我的应用程序位于纵向,我需要在 UIImagePickerController 中创建一些内容仅限 。如果我尝试使用:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

似乎我无法使用,因为我的Xcode在这一行中识别为错误(可能是因为Xcode的版本,我不确定)。而且我不确定,但似乎如果我正在开发 iOS 5.1 ,我还需要为iOS 6.0实现 ,这是正确的吗?

有人可以帮我理解制作 UIImagePickerController 需要什么,只有应用程序中的这个视图控制器可以在横向工作,在iOS 5.1和6中工作吗?

此致

3 个答案:

答案 0 :(得分:14)

根据UIImagePickerController Class Reference

  

重要UIImagePickerController课程仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,但有一个例外。您可以为cameraOverlayView属性分配自定义视图,并使用该视图显示其他信息或管理相机界面与代码之间的交互。

所以看起来强制横向模式不受支持和阻止,除非您通过使用自定义cameraOverlayView替换默认控件来执行此操作。

答案 1 :(得分:1)

创建一个名为“CUIImagePickerController”的类,继承自UIImagePickerController,覆盖以下方法,现在使用这个类!

@interface CUIImagePickerController : UIImagePickerController

@end

@implementation CUIImagePickerController
- (BOOL)shouldAutorotate {
    return NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
    return (orient == UIInterfaceOrientationLandscapeLeft) | (orient == UIInterfaceOrientationLandscapeRight);
}
@end

此致

答案 2 :(得分:-1)

尝试实施以下方法:

- (BOOL)shouldAutorotate
{

return YES;
}

-(NSUInteger)supportedInterfaceOrientations

{
return UIInterfaceOrientationMaskLandscape;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationLandscapeRight;
}
相关问题