1

I'm developing an Universal App in iOS SDK 8.3 and I'm using UISplitViewController. For some reasons I want to enable landscape orientation only for iPads and iPhone 6 Plus.

I can't find out a proper way to obtain this and for the moment I'm using this 'poor' work around that overrides a function of UIApplicationDelegateProtocol

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)
{
   if (window.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomUnspecified)
      return UIInterfaceOrientationMaskAll;

   if (window.traitCollection.userInterfaceIdiom == UIUserInterfaceIdiomPad)
      return UIInterfaceOrientationMaskAll;

   if (window.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular)
      return UIInterfaceOrientationMaskAllButUpsideDown;

   //iPhone 6 plus detected.
   if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
    && MAX([UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width) == 736)
      return UIInterfaceOrientationMaskAll;

   return UIInterfaceOrientationMaskPortrait;
}

Is there an elegant and 'safe' way to obtain the same result?

1 Answers1

0

Enable your Landscape Device Orientation in Deployment Info. Then put these macros for references:

 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
 #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

 #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
 #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
 #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
 #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

 #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
 #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
 #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
 #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
  1. Determine your preferred Interface Orientation in your ViewController

using these method:

- (BOOL) shouldAutorotate {
BOOL shouldRotate = (IS_IPAD || IS_IPHONE_6P) ? YES : NO;
return shouldRotate;
}

- (NSUInteger) supportedInterfaceOrientations {
 NSUInteger orientation = (IS_IPAD || IS_IPHONE_6P) ? UIInterfaceOrientationMaskLandscape : UIInterfaceOrientationMaskPortrait;
 return orientation;
 }

 - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
 UIInterfaceOrientation interface = (IS_IPAD || IS_IPHONE_6P) ? (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight) : UIInterfaceOrientationPortrait;
 return interface;
 }
Pepeng Hapon
  • 357
  • 1
  • 17