应用程序只启用肖像,但UIImagePickerController在iOS6中旋转

请注意下面的答案 – 不适用于iOS6,所以我仍然需要一个答案!

我的应用程序仅适用于肖像模式。

但是,如果我内嵌一个UIImagePickerController作为子视图,并旋转设备,顶部和底部酒吧留在相同的位置,但UIImagePickerController不会旋转。

我怎样才能防止它旋转?

这是代码:

[self.view.window addSubview:self.imagePickerController.view]; self.imagePickerController.showsCameraControls = NO; self.imagePickerController.view.frame = CGRectMake(0, 90, 320, 320); self.imagePickerController.allowsEditing = NO; 

EDITED

我正在使用iOS6,其中shouldAutorotate不是calle

在你的类中添加这个UIImagePickerController类别,

 @interface UIImagePickerController(Nonrotating) - (BOOL)shouldAutorotate; @end @implementation UIImagePickerController(Nonrotating) - (BOOL)shouldAutorotate { return NO; } @end 

包括以下在你的控制器这将工作,我只是创buildUIImagePickerController类别

 @interface UIImagePickerController (private) - (BOOL)shouldAutorotate; - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; - (NSUInteger)supportedInterfaceOrientations; - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; @end @implementation UIImagePickerController (Private) - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotate { return UIInterfaceOrientationMaskPortrait; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 

一种可能是重写

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; 

UIImagePickerController方法。 我不确定这是否是最好的可能性,但它会起作用。

所以,如果你只想让你的UIImagePickerController旋转到肖像使用下面的代码

 @interface PortraitUIImagePickerController : UIImagePickerController @end 

实现应该如下所示

 @implementation PortraitUIImagePickerController - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return UIInterfaceOrientationIsPortrait(toInterfaceOrientation); } @end 

大多数投票答案中的类别是有效的,但由于不鼓励使用类别,因此您也可以创buildUIImagePickerController的子类并使用它。

如果你想避免旋转的UIImagePickerController添加下面的类

UINonRotatableImagePickerController.h

 @interface UINonRotatableImagePickerController : UIImagePickerController @end 

UINonRotatableImagePickerController.m

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

您必须更改故事板中的UIImagePicker类以使用UILandscapeImagePickerController,或者如果您在代码中分配它,请更改

 UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 

 UIImagePickerController *picker = [[UINonRotatableImagePickerController alloc] init]; 

并在代码中包含UINonRotatableImagePickerController.h。