目标C语言 在ios8.3中,只有在某些设备上启用横向的正确方法



我正在iOS SDK 8.3中开发一个通用应用程序,我正在使用UISplitViewController。由于某些原因,我想只在ipad和iPhone 6 Plus上启用横向。

我找不到一个合适的方法来获得这个,目前我正在使用这个"可怜"的工作,它覆盖了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;
}

是否有一种优雅且"安全"的方法来获得相同的结果?

在部署信息中启用横向设备方向。然后放置这些宏作为引用:

 #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)
  • 在视图控制器中确定你的首选界面方向
  • 使用这些方法:

    - (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;
     }
    

    相关内容

    • 没有找到相关文章

    最新更新