Application is enabled only to Portrait, but UIIma

2019-06-19 01:03发布

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

My application is enabled only for Portrait mode.

However, if I embed a UIImagePickerController inside as a subview, and rotate the device, the top and bottom bar stays in the same location, however UIImagePickerController does rotate.

How can I prevent it from rotating?

This is the code:

    [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

I am using iOS6 where shouldAutorotate is not being calle

4条回答
爷、活的狠高调
2楼-- · 2019-06-19 01:31

Add this UIImagePickerController category in your class,

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

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {

  return NO;
}

@end
查看更多
地球回转人心会变
3楼-- · 2019-06-19 01:34

include the following in your controller this will work, I'm just creating the category of 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
查看更多
干净又极端
4楼-- · 2019-06-19 01:34

The category in the most voted answer works, but since it is discouraged to use categories, you can also make a subclass of UIImagePickerController and use that.

If you want to avoid rotating of the UIImagePickerController add the following class

UINonRotatableImagePickerController.h

@interface UINonRotatableImagePickerController : UIImagePickerController

@end

UINonRotatableImagePickerController.m

@implementation UINonRotatableImagePickerController

- (BOOL)shouldAutorotate
{
    return NO;
}

@end

You have to change the UIImagePicker class in the storyboard to use UILandscapeImagePickerController, or if you allocate it in code, change

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

to

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

and include UINonRotatableImagePickerController.h in your code.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-06-19 01:50

One possibility is to override the

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;

method of UIImagePickerController. I'm not sure if this is the best possibility but it will work.

So if you only want your UIImagePickerController to be rotated to portrait use the following code

@interface PortraitUIImagePickerController : UIImagePickerController

@end

And the implementation should look like the following

@implementation PortraitUIImagePickerController

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

@end
查看更多
登录 后发表回答