iOS7 iPad Landscape only app, using UIImagePickerController
Asked Answered
I

6

32

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".

Insufflate answered 9/12, 2013 at 10:31 Comment(0)
I
83

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

Insufflate answered 9/12, 2013 at 10:31 Comment(27)
What about if the App isn't landscape only?Juan
than it should be no problem, as the app support any orientation anywayInsufflate
You're not supposed to override a class's methods in a category. Which method actually gets used is technically undefined, even if it always seems to work. Apple could change something that would break it at a future point in time.Mitigate
"Which method actually gets used is technically undefined" thats not trueInsufflate
I have implemented all the things as discussed above but the thing is that my camera is rotated 90 degree and comes in half the screen when i switch to landscape orientation !!Anthelion
than you did smth. wrong. or smth. else is changing the bahaviour (note this is tested for iOS7)Insufflate
@ParvezBelim is right. Camera showing 90 Degree rotated.Yseult
than you did smth. wrong. or smth. else is changing the bahaviour (note this is tested for iOS7) –Insufflate
That worked for me like a charm. Thank u so much buddy. I was searching from couple of days.You saved my lots of time. Thanks again.Langbehn
IT WORKS IF YOU FOLLOW STEPS PROPERLY.Anstice
Will this pass appstore?Boardwalk
I followed the steps exactly and I can't get it to work in IOS7, has anyone gotten it working there?Deas
@ScottRowley 1) copy/paste method to app delegate 2) file -> new -> file -> objective-c category -> (Category) OrientationFix (Category on) UIViewController 3) file -> new -> file -> objective-c category -> (Category) OrientationFix (Category on) UIImagePickerControllerBoardwalk
That's exactly what I did :(Deas
@ScottRowley works for me on iOS7, for camera + photo album both.Luxe
This works on iOS7, we must implement in both UIViewController and UIImagePickerControllerSpotty
@Collin, this works great. Just a question. I replaced first with an plist setting (or setting in Target - General - Device Orientation), but that does not works, why?Ringer
The Perfect solution Peter...Thank you very much..!@!!Danelle
This works on both iPhone and iPad, with iOS7 and iOS8. I have implemented it into my project. Perfect.Ringer
This does not work on iOS 8.1.2. Have created a test project following the steps above here for anyone to try: github.com/ricsantos/image-picker-testTrowbridge
It Shows me This error. Warning: Attempt to present <UIImagePickerController: 0x7fcaec013400> on <MFSideMenuContainerViewController: 0x7fcaea4e8f20> which is already presenting <UIAlertController: 0x7fcaec90a220> Please help meMuncy
probably you have some other modal presented?Insufflate
Didn't need step 3 in iOS 8.4Autostrada
Worked fine in iOS 8.4. Didn't need step 3.Martyry
Hi @PeterLapisu Code is working for me but Portrait mode is getting enabled by this. I have game and i dont want to enable portrait mode in any situation. Any idea?Griffon
@smitpatel this shouldn't happen, looks like you have maybe some other problem there?Insufflate
this is the only route that worked for me with Xamarin.iOSHokusai
P
41

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.

Pensionary answered 17/4, 2015 at 5:22 Comment(11)
This is the best answer. Using categories is a hack. This works with photo, video and library source type. Tested with iOS8.3.Selfdefense
This answer is more beautiful than any that I've seen.Donatist
iOS 8.4: Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [PUUIAlbumListViewController shouldAutorotate] is returning YES'Autostrada
Work like charm on iOS 8.4.Windhoek
Works on iOS 8.4.1. We should vote more her and ask this to be marked as the correct answer.Harlandharle
IOS 9 Works! This is the correct answer. Also for Swift code: imagePicker.modalPresentationStyle = UIModalPresentationStyle.CurrentContextVictoria
Elsurudo, please note that you still need to tick Device Orientation Portrait to prevent the crash.Victoria
This didn't work for me. But imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen; did.Bering
This didn't work for me either on iOS 9.2 — it doesn't avoid the crash if unless you tick Portrait in your app's orientation settings.Stemma
It can present vc on iPad pro.But the displayed exception.Stanislaus
imagePickerController.modalPresentationStyle = .currentContext ... worked perfect for me in iOS10.3 & Swift3.0Blindstory
J
1

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

Juan answered 31/1, 2014 at 15:46 Comment(0)
F
0

Thanks to Peter Lapisu suggestion above. It doesn't work for me (maybe i'm on iOS 8.3) but i was able to modified it to fixed my orientation issues. My codes are below

In app delegate

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

UIImagePickerController category

@implement UIImagePickerController (extensions)
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[UIApplication sharedApplication] statusBarOrientation];
}

@end

UIViewController category

@implementation UIViewController (extensions)

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

@end
Fumikofumitory answered 11/5, 2015 at 4:7 Comment(0)
P
0

Although most answers recommend using .currentContext, I have found out after dismissing the imagepicker, everything was wrong.

On an Landscaped iPad, imho it's best if you would use .formSheet:

let picker = UIImagePickerController()
picker.modalPresentationStyle = .formSheet
picker.sourceType = .photoLibrary
picker.delegate = self
self.present(picker, animated: true)
Petrolic answered 19/10, 2017 at 9:59 Comment(0)
P
0

Swift 4 solution

Make sure that when you are having the ViewController embedded in a NavigationViewController to have the fix there. It won't work adding this restriction to the ViewController then...

import UIKit

class MainNavigationViewController: UINavigationController {

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
}

And of course the code mentioned above for the AppDelegate:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return .all
}

Btw this is only necessary for iOS 10 and below. Apple seems to have fixed it from iOS 11 and above...

Payer answered 12/11, 2018 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.