首页 文章

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

提问于
浏览
6

Please note that the answer below - do not work for iOS6 so I still need an answer!

我的应用程序仅在纵向模式下启用 .

但是,如果我将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不会被调用

4 回答

  • 12

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

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

    在你的控制器中包含以下内容,这将起作用,我只是创建了 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

    一种可能性是覆盖

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
    

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

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

    @interface PortraitUIImagePickerController : UIImagePickerController
    
    @end
    

    实现应如下所示

    @implementation PortraitUIImagePickerController
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
    }
    
    @end
    
  • 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 .

相关问题