Alessandro
Alessandro

Reputation: 4100

UIImagePickerController rotates when device rotated iOS 7

I have a UIImagePickerController and I would like its orientation to be kept in portrait mode at all times. The app itself is set in portrait mode, but the UIImagePickerController changes orientation. I have created a custom UIImagePickerController with the code:

#import "ViewControllerImage.h"

@interface ViewControllerImage ()
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end

@implementation ViewControllerImage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSUInteger)supportedInterfaceOrientations {

return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {

return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

And I present it in this way:

ViewControllerImage *picker = [[ViewControllerImage alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker animated:YES completion:nil];

The code doesn't work controller still rotates. Why?

Upvotes: 1

Views: 1451

Answers (1)

Sergey Demchenko
Sergey Demchenko

Reputation: 2954

As simplest solution - allow portrait mode in your proj file.

As a second solution - try to add the following code to you app delegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationPortrait;
}

Upvotes: 1

Related Questions