96

My app (iPad;iOS 6) is a landscape only application, but when I try using a UIPopoverController to display the photo library it throws this error: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES. I've tried changing a lot of the code around but I've had no luck.

iOS Dev
  • 4,143
  • 5
  • 30
  • 58
Destiny Dawn
  • 1,457
  • 3
  • 15
  • 29
  • 1
    you should accept an answer. there are much people for help and for other people who have the same issue it helps out when you mark the correct answer for your issue! – brush51 Nov 01 '12 at 14:50
  • 1
    No! no solution work on iOS 7 :( (headbang) – AsifHabib Apr 11 '14 at 08:03
  • Best answer here http://stackoverflow.com/questions/20468335/ios7-ipad-landscape-only-app-using-uiimagepickercontroller – Damien Romito Sep 04 '15 at 16:12

15 Answers15

98

In IOS6 you have supported interface orientations in three places:

  1. The .plist (or Target Summary Screen)
  2. Your UIApplicationDelegate
  3. The UIViewController that is being displayed

If you are getting this error it is most likely because the view you are loading in your UIPopover only supports portrait mode. This can be caused by Game Center, iAd, or your own view.

If it is your own view, you can fix it by overriding supportedInterfaceOrientations on your UIViewController:

- (NSUInteger) supportedInterfaceOrientations
{
     //Because your app is only landscape, your view controller for the view in your
     // popover needs to support only landscape
     return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

If it is not your own view (such as GameCenter on the iPhone), you need to make sure your .plist supports portrait mode. You also need to make sure your UIApplicationDelegate supports views that are displayed in portrait mode. You can do this by editing your .plist and then overriding the supportedInterfaceOrientation on your UIApplicationDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Snickers
  • 1,603
  • 11
  • 6
  • 3
    `UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight` equals `UIInterfaceOrientationMaskAllButUpsideDown` `UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight` equals `UIInterfaceOrientationMaskLandscape` – Valerii Pavlov Dec 06 '12 at 20:27
  • 4
    Or just use UIInterfaceOrientationMaskAll. – Mark Wang May 15 '13 at 17:17
  • Perfect. Even though a "portrait-only" popover will work in landscape, it can't present a landscape-only view controller modally. – Neal Ehardt Jul 01 '13 at 19:43
  • No! Solution did not worked on iOS 7 :(. http://stackoverflow.com/questions/23005351/supported-orientations-crash-ios7?noredirect=1#comment35138081_23005351 – AsifHabib Apr 11 '14 at 07:25
  • Worked on iOS 8. Though, had to add portrait support to plist. Thank you. – Yaroslav Apr 27 '15 at 13:08
  • My issue was with StoreKit on a landscape app. Adding portrait as a supported interface orientation in the AppDelegate file was the fix! – RanLearns Jan 12 '19 at 06:47
66

After spending a lot of time searching a way to avoid subclassing and adding ton of code, here's my one line code solution.

Create a new one UIImagePickerController's category and add

-(BOOL)shouldAutorotate{
    return NO;
}

That's all folks!

dandan78
  • 13,328
  • 13
  • 64
  • 78
Dr.Luiji
  • 6,081
  • 1
  • 15
  • 11
43

There is another case this error message may appear. I was searching for hours until I found the problem. This thread was very helpful after reading it a couple of times.

If your main view controller is rotated to landscape orientation and you invoke a custom sub view controller which should be displayed in portrait orientation this error message can happen when your code looks like this:

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationPortrait;
}

The trap here was xcode's intellisense suggested "UIInterfaceOrientationPortrait" and I didn't care about it. At the first glance this seemed to be correct.

The right mask is named

UIInterfaceOrientationMaskPortrait

Be aware of the small infix "Mask", else your subview will end up with an exception and the mentioned error message above.

The new enums are bit shifted. The old enums return invalid values!

(in UIApplication.h you can see the new declaration: UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait) )

The solution is:

- (BOOL)shouldAutorotate {

    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {

    // ATTENTION! Only return orientation MASK values
    // return UIInterfaceOrientationPortrait;

    return UIInterfaceOrientationMaskPortrait;
} 

In swift use

override func shouldAutorotate() -> Bool {

    return true
}

override func supportedInterfaceOrientations() -> Int {

    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
JackPearse
  • 2,922
  • 23
  • 31
  • 5
    happened to me for the same reason – ChenXin Mar 13 '13 at 06:05
  • I wish apple would just have made the return type UIInterfaceOrientationMask so that is a little more obvious what needs to be returned. – LightningStryk May 09 '14 at 17:44
  • Jackpearse 's answer looks correct. But when look into the info.plist, there 's UISupportedInterfaceOrientations with UIInterfaceOrientationPortrait, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight. No **MASK** here – onmyway133 Jun 27 '14 at 07:47
  • 1
    @onmyway: That's true, the plist values didn't change. In the iOS version before, Apple used the "non" mask values inside the code. This is some legacy stuff. I assume that apple is bit shifting the plist values internally now. – JackPearse Feb 03 '15 at 12:33
  • My app used to work without this crashing problem on an iPad simulator of Xcode 6.2. And it crashed after I upgraded to Xcode 6.3 a few days ago. Now this solution fixes my problem. Thx a lot :-) – Golden Thumb Apr 12 '15 at 22:07
22

I had a similar issue when presenting the image picker in a landscape only app. As suggested by Dr. Luiji's, I added the following category at the beginning of my controller.

// This category (i.e. class extension) is a workaround to get the
// Image PickerController to appear in landscape mode.
@interface UIImagePickerController(Nonrotating)
- (BOOL)shouldAutorotate;
@end

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {
  return NO;
}
@end

It's easiest to add these lines just before the @implementation of your ViewController .m file.

shawnwall
  • 4,549
  • 1
  • 27
  • 38
Trausti Kristjansson
  • 2,974
  • 2
  • 16
  • 11
  • I have a portrait only app! and I want to show Camera in Landscape mode. any solution????? – AsifHabib Apr 10 '14 at 14:40
  • Trausti not worked ? i have same issue and i tried your code but still appear same exception *** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES........ i also put a breakpoint on UIimagePickerView category method shouldAutorotate reaching there and returns no but still give this exception .... My app is Landscape only app... please help me – iamVishal16 Aug 01 '14 at 07:02
  • Also works to force SKStoreProductViewController to present in portrait when the whole app is landscape in iOS 8. Thanking you. – Yaroslav Aug 28 '15 at 21:27
11

I was encountering the same error message in my code. I found this, it's a bug as reported by Apple:

https://devforums.apple.com/message/731764#731764

His solution is to fix it in the AppDelegate. I implemented it and it works for me!

Robby
  • 315
  • 2
  • 11
  • By the way, I originally found this at: http://stackoverflow.com/questions/12522491/crash-on-presenting-uiimagepickercontroller-under-ios6/ – Robby Jan 22 '13 at 23:38
  • This was the right answer, direct from Apple, and then I just had to follow the same instructions but do it in Swift for an app targeting iOS 8. Everything works great. AppDelegate sets supportedInterfaceOrientationsForWindow to both Landscape and Portrait, each individual swift file sets override func supportedInterfaceOrientations to Landscape only so the view won't rotate, but when a portrait view (in my case SKStoreProductViewController) needs to load, it works! – RanLearns Mar 15 '15 at 03:39
  • Jeez, there are so many suggested solutions, and this one by far does not have the highest points, but it is indeed the correct one. Tested on iOS 10 and 9. Nothing else works. – Demian Turner Apr 21 '17 at 11:08
6

I had the same problem and this answer https://stackoverflow.com/a/12523916 works for me. Wonder if there is a more elegant solution.

My code:

UIImagePickerController  *imagePickerController = [[NonRotatingUIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

UIPopoverController  *popoverVC = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];    

[popoverVC presentPopoverFromRect:frame   // did you forget to call this method?
                           inView:view
         permittedArrowDirections:UIPopoverArrowDirectionAny
                         animated:YES];
Community
  • 1
  • 1
poetowen
  • 61
  • 3
  • I tried doing that but when I click the button to call the code but nothing displayed, also I tried adding `@interface NonRotatingUIImagePickerController : UIImagePickerController @end @implementation NonRotatingUIImagePickerController - (BOOL)shouldAutorotate { return NO; } @end` to the code but my code wouldn't detect NonRotatingUIImagePickerController. – Destiny Dawn Sep 22 '12 at 18:43
  • 1
    Never mind, It detects NonRotatingUIImagePickerController now but nothing displays still... @poetowen – Destiny Dawn Sep 22 '12 at 18:47
4
- (BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

This removes the crash.
Suhail Bhat
  • 443
  • 4
  • 13
4

iOS 8 - you can use UIModalPresentationPopover without any hacks to display in a popover. Not ideal but better than nothing.

imagePicker.modalPresentationStyle = UIModalPresentationPopover;
imagePicker.popoverPresentationController.sourceView = self.view;
imagePicker.popoverPresentationController.sourceRect = ((UIButton *)sender).frame;

Edit: perhaps try the different UIModalPresentationStyles - maybe more will work in landscape.

derbs
  • 333
  • 3
  • 6
2

Another option that resolved my issues was to create a subclass of the UIImagePickerController and to override the below method

@interface MyImagePickerController ()

@end

@implementation MyImagePickerController

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

Use this instead of the UIImagePickerController and it all works fine.

1

Creating a category is really helpful to fix this bug. And do not forget to import your created category. This will add the missing method to UIImagePickerController and on iOS 6 it will restrict it to work in Portrait only as the documentation states btw.

The other solutions may have worked. But with SDK for iOS 8.x compiled to deploy on iOS 6.1 this seems the way to go.

The .h-file:

#import <UIKit/UIKit.h>

@interface UIImagePickerController (iOS6FIX)

- (BOOL) shouldAutorotate;
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation;

@end

The .m-file:

#import "UIImagePickerController+iOS6FIX.h"

@implementation UIImagePickerController (iOS6FIX)

- (BOOL) shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end
Helge Staedtler
  • 101
  • 1
  • 4
1

Swift 3

let imagePicker = UIImagePickerController()

imagePicker.modalPresentationStyle = .popover
imagePicker.popoverPresentationController?.sourceView = sender // you can also pass any view 

present(imagePicker, animated: true)
zombie
  • 5,069
  • 3
  • 25
  • 54
1

Swift 4 and up assuming the whole app is in landscape and you need to present a single controller in portrait. In the view controller that has to be portrait add the following :

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

open override var shouldAutorotate: Bool {
    return false
}
Gal Blank
  • 2,070
  • 2
  • 18
  • 18
  • I was having this problem only when running my app in a non debug build. This answer solved the issue for me. – Marc Apr 27 '21 at 20:32
0

I encountered this crash issue when converting UIInterfaceOrientationPortrait to UIInterfaceOrientationMaskPortrait implicitly as a return value.

More code background on UIPageViewControllerDelegate, just FYI for all of you.

 -(UIInterfaceOrientationMask)pageViewControllerSupportedInterfaceOrientations:
(UIPageViewController *)pageViewController
{
    # return UIInterfaceOrientationPortrait;    # wrong
    return UIInterfaceOrientationMaskPortrait;  # correct
}
Itachi
  • 5,777
  • 2
  • 37
  • 69
0

I've just resolved the issue for Swift 4.x

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    configureVideoOrientation()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: nil, completion: { [weak self] _ in
        self?.configureVideoOrientation()
    })
}

private func configureVideoOrientation() {
    guard let previewLayer = self.previewLayer, let connection = previewLayer.connection else { return }
    if connection.isVideoOrientationSupported {
        let orientation = UIApplication.shared.statusBarOrientation
        switch (orientation) {
        case .portrait:
            previewLayer.connection?.videoOrientation = .portrait
        case .landscapeRight:
            previewLayer.connection?.videoOrientation = .landscapeRight
        case .landscapeLeft:
            previewLayer.connection?.videoOrientation = .landscapeLeft
        case .portraitUpsideDown:
            previewLayer.connection?.videoOrientation = .portraitUpsideDown
        default:
            previewLayer.connection?.videoOrientation = .portrait
        }

        previewLayer.frame = self.view.bounds
    }
}

Thank you guys for your answers also. I've just cut the worked code and simply refactored it.

atereshkov
  • 4,311
  • 1
  • 38
  • 49
0

Swift 5.3 iOS 16

First step Add this method and variable in app delegate

 var restrictRotation:UIInterfaceOrientationMask = .portrait

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

Second Step In your desired view controller where you want to rotate device orientation just simply change that var in app delegate like this

 override func viewDidLoad() {
    super.viewDidLoad()
    (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .landscapeLeft
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
} 

override var shouldAutorotate: Bool {
    return true
}

Before going back to particular controller just change that var again

 (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .portrait
 self.dismiss(animated: true) // for dismissing the controller
desertnaut
  • 57,590
  • 26
  • 140
  • 166