如何检测IOS 7和IOS 8和宽屏iPhone的大小,使我的应用程序通用?

我正在开发所有设备和IOS 7和IOS 8的通用IOS应用程序。我有这个macros:

这个macros是用来检测宽屏幕的iPhone 5的,这适用于IOS 7:

#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) 

这个macros也适用于宽屏幕iPone 5,但只适用于IOS 8:

 #define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON ) 

我需要结合这个代码使它在IOS 7和IOS 8上工作,为此我需要select器来检测IOS版本,下面是代码:

 #define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) #define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON ) #define IS_WIDESCREEN ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_WIDESCREEN_IOS8 : IS_WIDESCREEN_IOS7 ) 

那么这个post的作者build议引用 – “如果你也瞄准iOS 7或更低,一定要使用function检测,因为在iOS 8之前调用nativeBounds会导致你的应用程序崩溃:”并提供以下代码:

 if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) { /* Detect using nativeBounds - iOS 8 and greater */ } else { /* Detect using bounds - iOS 7 and lower */ } 

请帮助我在这里我是一个初学者的开发人员,并希望了解,以使其工作。 我应该把SKSpriteNode * background = [SKSpriteNode spriteNodeWithImageNamed:@“Background”] ;?

所有这些代码是从不同的post在这里是Stackoverflow的是: 如何检测iPhone 5(宽屏设备)?

我上传的图片下拉框这里是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0该文件夹被称为measuredImages。 这里是我用来添加背景的代码:#import“GameScene.h”

 @implementation GameScene -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background-568"]; background.anchorPoint = CGPointMake(0.5, 1); background.position = CGPointMake(self.size.width/2, self.size.height); [self addChild:background];} return self; } 

如果有人可以把macros的完整代码和用法在回答中,我将不胜感激。

重要更新:12.17.2014

正如Daij-Djanbuild议的那样,通过包含正确的启动图像和我的应用程序以正确的分辨率运行,我使用了屏幕边界[与ios7相同]解决了这个问题。 感谢大家帮助我解决这个问题,我个人要感谢Daij-Djan和sha的帮助和支持。 如果你需要宽屏iPhone的代码,我会在下面的自己的答案,它运行在iPhone 4上的所有iPhone和所有的iPad。

你不需要为了检测屏幕宽度而使用os。

只是包括正确的启动图像,你的应用程序将运行在正确的分辨率,你可以使用屏幕边界[与ios7相同]

我再次强调:包括正确的启动图像! 那么使用UIScreen bounds

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LaunchImages.html

当我需要一个快速和肮脏的方式来检测iOS7 / 8和iPhone / iPad设备时,我将使用以下macros:

 #define IS_IOS8 ([[UIDevice currentDevice].systemVersion compare:@"8.0" options:NSNumericSearch] != NSOrderedAscending) #define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 

你可以使用这些macros:

 if (IS_IPHONE) { // iPhone specific code ... } else { // iPad specific code ... } if (IS_IOS8) { // Code specific to iOS8+ ... } else { // Code specific to earlier versions of iOS ... } 

更新:要检测宽屏幕设备,你可以使用下面的macros(因为iOS8的UIScreen将是方向感知和高度将不同的纵向/横向,所以你可以检查两个:

 #define IS_WIDESCREEN (( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) || ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.width - ( double )568 ) < DBL_EPSILON )) 

使用这是非常有用的

 #define IS_IPHONE ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 480) #define IS_IPHONE5 ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 568) #define IS_IPHONE6 ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 667) #define IS_IPHONE6PLUS ((int)(MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) == 736) 

要检测iOS版本,您可以使用以下macros之一:

 #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) 

例:

 if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { // code here } 

而要检测iPhonetypes,您可以使用,

 CGSize applicationFrameSize = [UIScreen mainScreen].bounds.size; CGFloat maxHeight = (MAX(applicationFrameSize.width, applicationFrameSize.height)); _is4GDevice = (maxHeight == 480.0 || maxHeight == 480.0); _is5GDevice = (maxHeight == 568.0 || maxHeight == 568.0); _is6GDevice = (maxHeight == 667.0 || maxHeight == 667.0); _is6PlusDevice = (maxHeight == 736.0 || maxHeight == 736.0); 

好,所以解决这个问题的方法是将正确的启动图像包含到image.xcassets或LaunchImage.xib中,编译器会像Daij-Djan在上面的答案中提到的那样为您的图像select合适的大小屏幕。 为了使它适用于宽屏iPhone和所有iPhone(4,4及以上)和iPad和IOS 7和IOS 8.将这些macros添加到您的MyScene.m文件或​​您使用它的任何.m文件。

  #define IS_WIDESCREEN_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) #define IS_WIDESCREEN_6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON ) 

并使用此代码检测宽屏iPhone,这适用于所有IOS设备和IOS 7和IOS 8:

  -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { SKSpriteNode *background; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { if (IS_WIDESCREEN_5) { //detects WIDESCREEN iPhone 5,5c,5s for both IOS 7,8 background= [SKSpriteNode spriteNodeWithImageNamed:@"Background-568"]; }else if (IS_WIDESCREEN_6){ //detects WIDESCREEN iPhone 6 for both IOS 7,8 background= [SKSpriteNode spriteNodeWithImageNamed:@"Background-667"]; }else{ //detects iPhone 4,4s,iPhone 6 Plus, for both IOS 7,8 background= [SKSpriteNode spriteNodeWithImageNamed:@"Background"]; } }else{ //detects iPads all sizes and resolutions (Ipad regular display and iPad retina display) background= [SKSpriteNode spriteNodeWithImageNamed:@"Background~iPad"]; } background.anchorPoint = CGPointMake(0.5, 1); background.position = CGPointMake(self.size.width/2, self.size.height); [self addChild:background]; } return self; } 

最后一步就是用这种方式命名你的图像:iPhone 4.4s的Background@2x.png,宽屏iPhone 5,5c,5s的Background-568 @ 2x,宽屏iPhone 6的Background667@2x.png,背景@ 3x .png为iPhone 6 Plus,Background〜iPad.png为iPad常规显示,最后为iPad Retina Display的背景~iPad@2x.png。 您可以从保存箱中下载针对特定屏幕尺寸优化的图像。 这里是链接https://www.dropbox.com/sh/pnll2e2jvo0uigs/AACOLbzzQqZlJEZZcBx7TMR1a?dl=0并尝试它&#x3002; 最后也是最重要的是为每个屏幕尺寸添加启动图像,否则代码将无法工作。 我希望这有助于和感谢你们教我所有这一切我失去了2个月,让它工作,因为我有错误的信息。

它适用于5c&6&6plus。 它会检查屏幕是否是16:9。我是一个新的iOS程序员,请指教

 bool IsTargetDeviceWideScreen() { double screenWidth = 0; double screenHeight = 0; if ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) { CGSize screenSize = [ [ UIScreen mainScreen ] nativeBounds ].size; screenWidth = screenSize.width; screenHeight = screenSize.height; } else { CGSize screenSize = [ [ UIScreen mainScreen ] bounds ].size; screenWidth = screenSize.width; screenHeight = screenSize.height; } NSLog(@"screen size"); NSLog(@"%f", screenWidth); NSLog(@"%f", screenHeight); double rateWidthHeight = 0; if (screenWidth < screenHeight) { rateWidthHeight = (screenWidth * 16) / (screenHeight * 9); } else { rateWidthHeight = (screenWidth * 9) / (screenHeight * 16); } NSLog(@"%f", rateWidthHeight); if ( 0.99 < rateWidthHeight & rateWidthHeight < 1.01) { return true; } return false; }