5

In my universal app i need to handle different orientation for iphone and ipad. For ipad i need to allow landscape orientation and for iphone portrait alone. I have returned below code at first

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

is working fine in IOS 5 But in IOS 6 autorotate method is not at all fired. After that i have changed the method to,

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

even this methods are not at all fired in IOS 6.

My plist setting is

enter image description here

i need to handle both orientation [iPhone-portrait,iPad-landscape] for both IOS 5 and IOS 6. Please guide me to fix this issue.

Ganapathy
  • 4,594
  • 5
  • 23
  • 41
  • please see So My answer http://stackoverflow.com/questions/12933089/i-want-to-make-my-appliaction-only-in-landscape-orientation-in-ios-both-ios-5-a – Deepesh Jan 04 '13 at 06:36
  • In addition to the accepted answer, `preferredInterfaceOrientationForPresentation` asks for a single orientation and you are returning a mask (`UIInterfaceOrientationMaskPortrait`) instead of e.g. `UIInterfaceOrientationPortrait`. – Jesper Jun 19 '13 at 11:07

4 Answers4

3

Is your rootviewcontroller a UINavigation controller or UITabbarcontroller?? If so these methods wont work if you are calling these methods in your view controller.

So create an objective C category on these container view controllers and add to your project.

@implementation UINavigationController (autorotate)


 -(NSUInteger)supportedInterfaceOrientations
 {
   //make the check for iphone/ipad here

if(IPHONE)
    {
return UIInterfaceOrientationMaskPortrait;
    } 
else
    {
return UIInterfaceOrientationMaskLandscape;
    }

 }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationPortrait;
 }

 - (BOOL)shouldAutorotate
 {
return NO;
 }
Xcoder
  • 1,762
  • 13
  • 17
2

add this method to your app delegate its 100% working

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationPortraitUpsideDown;
    }
    else
        return  UIInterfaceOrientationMaskAll;
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
0

What you want is the following:

#pragma mark - iOS 5.1 and under Rotation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
  }
  else{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
  }
}

#pragma mark - iOS 6.0 and up Rotation Methods

- (BOOL)shouldAutorotate; {
  return YES;
}

- (NSUInteger)supportedInterfaceOrientations; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  }
  else{
    return UIInterfaceOrientationMaskLandscape;
  }
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
  if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    return UIInterfaceOrientationPortrait;
  }
  else{
    return UIInterfaceOrientationLandscapeLeft;
  }
}

What you have to make sure of is 2-fold.

First, that this is implemented in you RootViewController. If you are embedding your UIViewController's inside a UINavigationController (or a UITabBarController), then you must make a custom UINavigationController (or a custom UITabBarController) class that implements these methods and use that.

Second, you must make sure that you have the orientations supported in you Info.plist file. Otherwise, the system will override the returned values of these methods (Note: you can easily change these values by clicking the buttons in you targets Summary page as well).

Hope that Helps!

msgambel
  • 7,320
  • 4
  • 46
  • 62
0

Here's the swift version of Pradeep's answer. You don't need to subclass all of your navigation controllers to get what you want to work (aka, turn off landscape orientation for iPhone only)

Just add this to your app delegate:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}
Travis M.
  • 10,930
  • 1
  • 56
  • 72