31

This error doesn't make sense, as the preferred orientation UIInterfaceOrientationLandscapeRight is returned by the supported orientation

//iOS6

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Error :

Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179

4 Answers4

57

Your code should look like this:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

Also, make sure in your Info.plist you have set the correct orientations for you app because what you return from supportedInterfaceOrientations is intersected with the Info.plist and if it can't find a common one then you'll get that error.

lmirosevic
  • 15,787
  • 13
  • 70
  • 116
  • I'm finding this to cause me grief! I have an universal app sharing viewcontroller code and use the above code testing for user idiom. the ipad need to be only landscape and the iphone portrait for all bar one that needs to be landscape. I cannot get this to deliver the correct orientations at – user7865437 Nov 21 '12 at 12:05
  • 4
    Note that it is the "Mask" part of the "UIInterfaceOrientationMaskLandscape" that is the important part of this answer. The original poster user the wrong enum in his method. It seems kinda silly that Apple created a new set of enum/optionss for this method leading to people making this easy error - additionally Xcode does not even provide any compiler time checking because the method returns NSUInteger. – Corey Floyd Mar 31 '13 at 00:21
  • 1
    @lms, My entire application should support to only for Portrait mode except one View(which need to support for Landscape). In Plist I have set orientation only for Portrait and i wrote above code in where i want to change orientation for Landscape. But its giving either UIInterfaceOrientationLandscapeRight Or UIInterfaceOrientationLandscapeLeft.But i want both in my view. Can u plz tel me how can i get it. – Ganesh G Jul 02 '13 at 06:11
13

supportedInterfaceOrientations is only called, if shouldAutorotate is set to YES

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

The easiest approach for me, is only to set the Info.plist

info.plist

If you like to support iOS 5 use this code in your view controllers.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
coco
  • 4,912
  • 2
  • 25
  • 33
9

Those are the wrong enums for supportedInterfaceOrientations. You need to use UIInterfaceOrientationMaskLandscapeLeft, etc (note the word mask in the middle)

borrrden
  • 33,256
  • 8
  • 74
  • 109
1

from the documentation:

-(NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
}

Note that the correct orientation is "Mask"! Did you try this?

Panayotis
  • 1,792
  • 23
  • 32