Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS7 iPad Landscape only app, using UIImagePickerController

I believe this is a common issue and many answers don't work anymore, many just partial, if you are under iOS7 and your iPad app is Landscape only, but you want to use the UIImagePickerController with source UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeCamera.

How to set it right, so it's working 100%? And you don't get mixed orientations and avoid the error "Supported orientations has no common orientation with the application, and shouldAutorotate returns YES".

like image 391
Peter Lapisu Avatar asked Dec 09 '13 10:12

Peter Lapisu


3 Answers

If your iPad app is landscape only in all conditions, just do these 3 steps :

1) In your app delegate

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

2) Create a category header

#import "UIViewController+OrientationFix.h"

@implementation UIViewController (OrientationFix)

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

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

3) Create a category implementation

#import "UIImagePickerController+OrientationFix.h"

@implementation UIImagePickerController (OrientationFix)

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

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end

Note: You don't need to import these categories anywhere, just enough they are compiled with the project

Note: no need to implement these methods in any VC

Note: no need to change your plist supported orientations

This is tested and working under any conditions

like image 185
Peter Lapisu Avatar answered Nov 16 '22 12:11

Peter Lapisu


I have seen this code from Apple's Sample Code.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;

Due to this UIModalPresentationCurrentContext UIImagePickerController will be opened as per device's current orientation.

like image 41
Chintan Prajapati Avatar answered Nov 16 '22 14:11

Chintan Prajapati


Apple's documentation says:

"Important: The UIImagePickerController class supports portrait mode only."

Although, it works fine in landscape for full screen and on iOS 6.

UIImagePickerController class reference

like image 1
Collin Avatar answered Nov 16 '22 14:11

Collin