6

请注意下面的答案 - 不适用于 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;

已编辑

我正在使用未调用 shouldAutorotate 的 iOS6

4

4 回答 4

12

在您的课程中添加此UIImagePickerController类别,

@interface UIImagePickerController(Nonrotating)
- (BOOL)shouldAutorotate;
@end

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {

  return NO;
}

@end
于 2013-01-21T11:16:12.943 回答
2

在您的控制器中包含以下内容,这将起作用,我只是在创建类别UIImagePickerController

@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
于 2013-01-21T11:27:58.750 回答
0

投票最多的答案中的类别有效,但由于不鼓励使用类别,您也可以创建 UIImagePickerController 的子类并使用它。

如果要避免 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。

于 2014-04-03T10:49:32.797 回答
0

一种可能性是覆盖

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;

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

因此,如果您只想将 UIImagePickerController 旋转为纵向,请使用以下代码

@interface PortraitUIImagePickerController : UIImagePickerController

@end

并且实现应该如下所示

@implementation PortraitUIImagePickerController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

@end
于 2013-01-21T11:18:46.287 回答