应用程序仅对Portrait启用,但UIImagePickerController在iOS6中旋转

时间:2013-01-21 10:58:32

标签: iphone ios objective-c cocoa-touch uiimagepickercontroller

请注意以下答案 - 对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

4 个答案:

答案 0 :(得分:12)

在班级中添加此UIImagePickerController类别

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

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {

  return NO;
}

@end

答案 1 :(得分: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

答案 2 :(得分:0)

一种可能性是覆盖

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;

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

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

@interface PortraitUIImagePickerController : UIImagePickerController

@end

实现应如下所示

@implementation PortraitUIImagePickerController

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

@end

答案 3 :(得分: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。