How to detect iPhone 5 (widescreen devices)?
我刚升级到Xcode4.5gm,发现你现在可以将"4"视网膜大小应用到故事板中的视图控制器。
现在,如果我想创建一个同时在iPhone4和iPhone5上运行的应用程序,当然我必须构建每个窗口两次,但我还必须检测用户是否有一个屏幕为3.5英寸或4英寸的iPhone,然后应用该视图。
我该怎么做?
首先,您不应该重建所有视图以适应新屏幕,也不应该针对不同的屏幕大小使用不同的视图。
使用iOS的自动调整大小功能,以便您的视图可以调整和适应任何屏幕大小。
这并不难,阅读一些相关文档。这会节省你很多时间。
iOS6也提供了新功能。一定要阅读苹果开发者网站上的iOS6 API变更日志。并检查新的iOS6自动布局功能。
也就是说,如果你真的需要检测iPhone5,你可以简单地依靠屏幕大小。
1 | [ [ UIScreen mainScreen ] bounds ].size.height |
iPhone5的屏幕高度为568。您可以想象一个宏,以简化所有这些操作:
1 | #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) |
如H2CO3评论中指出的,在比较浮点时,使用
从现在起,您可以在标准if/else语句中使用它:
1 2 3 4 | if( IS_IPHONE_5 ) {} else {} |
编辑-更好的检测
正如一些人所说,这只检测到宽屏,而不是真正的iPhone5。
iPod touch的下一个版本可能也会有这样的屏幕,所以我们可以使用另一组宏。
让我们将原来的宏重命名为
1 | #define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON ) |
让我们添加模型检测宏:
1 2 | #define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] ) #define IS_IPOD ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] ) |
这样,我们可以确保我们有一个iPhone型号和一个宽屏,我们可以重新定义
1 | #define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN ) |
另外请注意,如@learncocos2d所述,如果应用程序未针对iPhone 5屏幕进行优化(缺少默认的[email protected]图像),则此宏将无法工作,因为在这种情况下,屏幕大小仍将为320x480。
我不认为这是个问题,因为我不明白为什么我们要在一个非优化的应用程序中检测iPhone5。
重要信息-iOS 8支持
在iOS 8上,
为了解决这个问题,您可以简单地使用新的
如果您的目标是iOS 7或更低版本,请务必使用功能检测,因为在iOS 8之前调用
1 2 3 4 5 6 7 8 | if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) { /* Detect using nativeBounds - iOS 8 and greater */ } else { /* Detect using bounds - iOS 7 and lower */ } |
可以按以下方式调整以前的宏:
1 2 3 | #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 ) |
显然,如果您需要检测iPhone6或6 Plus,请使用相应的屏幕尺寸。
针对任何SDK和OS组合进行测试和设计:
迅捷增加了iPad类型。ipad 2和ipad mini是非视网膜型ipad。虽然ipad mini 2及以上版本,但ipad 3、4、ipad air、air 2、air 3和ipad pro 9.7的逻辑分辨率相同,为1024。iPad Pro的最大长度为1366。参考文献
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import UIKit public enum DisplayType { case unknown case iphone4 case iphone5 case iphone6 case iphone6plus case iPadNonRetina case iPad case iPadProBig static let iphone7 = iphone6 static let iphone7plus = iphone6plus } public final class Display { class var width:CGFloat { return UIScreen.main.bounds.size.width } class var height:CGFloat { return UIScreen.main.bounds.size.height } class var maxLength:CGFloat { return max(width, height) } class var minLength:CGFloat { return min(width, height) } class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale } class var retina:Bool { return UIScreen.main.scale >= 2.0 } class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone } class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad } class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay } class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv } class var typeIsLike:DisplayType { if phone && maxLength < 568 { return .iphone4 } else if phone && maxLength == 568 { return .iphone5 } else if phone && maxLength == 667 { return .iphone6 } else if phone && maxLength == 736 { return .iphone6plus } else if pad && !retina { return .iPadNonRetina } else if pad && retina && maxLength == 1024 { return .iPad } else if pad && maxLength == 1366 { return .iPadProBig } return .unknown } } |
行动起来看看https://gist.github.com/hbossli/bc93d924649de881ee2882457f14e346
注:如果iPhone6处于缩放模式,则用户界面是iPhone5的放大版本。这些功能不确定设备类型,但显示模式,因此在本例中,iPhone5是所需的结果。
Objtovi-C1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #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_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) #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) |
用法:http://pastie.org/9687735
注:如果iPhone6处于缩放模式,则用户界面是iPhone5的放大版本。这些功能不确定设备类型,但显示模式,因此在本例中,iPhone5是所需的结果。
非常简单的解决方案
1 2 3 4 5 6 7 8 9 10 11 12 | if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; if(result.height == 480) { // iPhone Classic } if(result.height == 568) { // iPhone 5 } } |
我们现在需要考虑iPhone6和6plus的屏幕尺寸。这是最新的答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //its iPhone. Find out which one? CGSize result = [[UIScreen mainScreen] bounds].size; if(result.height == 480) { // iPhone Classic } else if(result.height == 568) { // iPhone 5 } else if(result.height == 667) { // iPhone 6 } else if(result.height == 736) { // iPhone 6 Plus } } else { //its iPad } |
一些有用的信息
1 2 3 4 5 | iPhone 6 Plus 736x414 points 2208x1242 pixels 3x scale 1920x1080 physical pixels 401 physical ppi 5.5" iPhone 6 667x375 points 1334x750 pixels 2x scale 1334x750 physical pixels 326 physical ppi 4.7" iPhone 5 568x320 points 1136x640 pixels 2x scale 1136x640 physical pixels 326 physical ppi 4.0" iPhone 4 480x320 points 960x640 pixels 2x scale 960x640 physical pixels 326 physical ppi 3.5" iPhone 3GS 480x320 points 480x320 pixels 1x scale 480x320 physical pixels 163 physical ppi 3.5" |
我冒昧地将Macmade的宏放到了C函数中,并正确地命名它,因为它检测到宽屏可用性,而不一定是iPhone5。
如果项目中没有默认的[email protected],宏也不会检测到在iPhone5上运行。如果没有新的默认图像,iPhone5将报告常规的480x320屏幕大小(以磅为单位)。因此,不仅要检查宽屏可用性,还要检查启用宽屏模式的情况。
1 2 3 4 5 | BOOL isWidescreenEnabled() { return (BOOL)(fabs((double)[UIScreen mainScreen].bounds.size.height - (double)568) < DBL_EPSILON); } |
以下是我们的代码,通过iOS7/iOS8测试的iPhone4、iPhone5、iPad、iPhone6、iPhone6p,无论在设备或模拟器上:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) // iPhone and iPod touch style UI #define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f) #define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f) #define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f) #define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f) #define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f) #define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f) #define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f) #define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f) #define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 ) #define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 ) #define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 ) #define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 ) |
我用hbossli的答案翻译成了swift
1 2 3 4 5 6 7 8 9 10 11 12 13 | let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad let IS_IPHONE = UIDevice.currentDevice().userInterfaceIdiom == .Phone let IS_RETINA = UIScreen.mainScreen().scale >= 2.0 let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height let SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT) let SCREEN_MIN_LENGTH = min(SCREEN_WIDTH, SCREEN_HEIGHT) let IS_IPHONE_4_OR_LESS = (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) let IS_IPHONE_5 = (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) let IS_IPHONE_6 = (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) let IS_IPHONE_6P = (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) |
这是我的cocos2d项目的宏。其他应用程序应该相同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #define WIDTH_IPAD 1024 #define WIDTH_IPHONE_5 568 #define WIDTH_IPHONE_4 480 #define HEIGHT_IPAD 768 #define HEIGHT_IPHONE 320 #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //width is height! #define IS_IPHONE_5 ( [ [ UIScreen mainScreen ] bounds ].size.height == WIDTH_IPHONE_5 ) #define IS_IPHONE_4 ( [ [ UIScreen mainScreen ] bounds ].size.height == WIDTH_IPHONE_4 ) #define cp_ph4(__X__, __Y__) ccp(cx_ph4(__X__), cy_ph4(__Y__)) #define cx_ph4(__X__) (IS_IPAD ? (__X__ * WIDTH_IPAD / WIDTH_IPHONE_4) : (IS_IPHONE_5 ? (__X__ * WIDTH_IPHONE_5 / WIDTH_IPHONE_4) : (__X__))) #define cy_ph4(__Y__) (IS_IPAD ? (__Y__ * HEIGHT_IPAD / HEIGHT_IPHONE) : (__Y__)) #define cp_pad(__X__, __Y__) ccp(cx_pad(__X__), cy_pad(__Y__)) #define cx_pad(__X__) (IS_IPAD ? (__X__) : (IS_IPHONE_5 ? (__X__ * WIDTH_IPHONE_5 / WIDTH_IPAD) : (__X__ * WIDTH_IPHONE_4 / WIDTH_IPAD))) #define cy_pad(__Y__) (IS_IPAD ? (__Y__) : (__Y__ * HEIGHT_IPHONE / HEIGHT_IPAD)) |
1 2 3 4 5 6 | if ((int)[[UIScreen mainScreen] bounds].size.height == 568) { // This is iPhone 5 screen } else { // This is iPhone 4 screen } |
在Swift中,iOS 8+项目我喜欢在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | extension UIScreen { var isPhone4: Bool { return self.nativeBounds.size.height == 960; } var isPhone5: Bool { return self.nativeBounds.size.height == 1136; } var isPhone6: Bool { return self.nativeBounds.size.height == 1334; } var isPhone6Plus: Bool { return self.nativeBounds.size.height == 2208; } } |
(注:
然后代码将是:
1 2 3 | if UIScreen.mainScreen().isPhone4 { // do smth on the smallest screen } |
代码清楚地表明这是对主屏幕的检查,而不是对设备型号的检查。
借用SamratMazumdar的答案,这里有一个简短的方法来估计设备屏幕大小。它适用于最新的设备,但在未来的设备上可能会失败(就像所有猜测方法一样)。如果正在镜像设备,也会混淆(返回设备的屏幕大小,而不是镜像的屏幕大小)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #define SCREEN_SIZE_IPHONE_CLASSIC 3.5 #define SCREEN_SIZE_IPHONE_TALL 4.0 #define SCREEN_SIZE_IPAD_CLASSIC 9.7 + (CGFloat)screenPhysicalSize { if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { CGSize result = [[UIScreen mainScreen] bounds].size; if (result.height < 500) return SCREEN_SIZE_IPHONE_CLASSIC; // iPhone 4S / 4th Gen iPod Touch or earlier else return SCREEN_SIZE_IPHONE_TALL; // iPhone 5 } else { return SCREEN_SIZE_IPAD_CLASSIC; // iPad } } |
我认为如果这个宏能在设备和模拟器中工作应该是好的,下面是解决方案。
1 2 3 4 | #define IS_WIDESCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON) #define IS_IPHONE (([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"]) || ([[[UIDevice currentDevice] model] isEqualToString: @"iPhone Simulator"])) #define IS_IPOD ([[[UIDevice currentDevice]model] isEqualToString:@"iPod touch"]) #define IS_IPHONE_5 ((IS_IPHONE || IS_IPOD) && IS_WIDESCREEN) |
我发现答案不包括模拟器的特殊情况。
1 2 3 4 | #define IS_WIDESCREEN ( [ [ UIScreen mainScreen ] bounds ].size.height == 568 ) #define IS_IPHONE ([[ [ UIDevice currentDevice ] model ] rangeOfString:@"iPhone"].location != NSNotFound) #define IS_IPAD ([[ [ UIDevice currentDevice ] model ] rangeOfString:@"iPad"].location != NSNotFound) #define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | +(BOOL)isDeviceiPhone5 { BOOL iPhone5 = FALSE; CGRect screenBounds = [[UIScreen mainScreen] bounds]; if (screenBounds.size.height == 568) { // code for 4-inch screen iPhone5 = TRUE; } else { iPhone5 = FALSE; // code for 3.5-inch screen } return iPhone5; } |
在Swift 3中,您可以使用我的简单类krdevicetype。
网址:https://github.com/ulian-onua/krdevicetype
它有很好的文档记录并支持操作符=、>=、<=。
例如,要检测设备是否有iPhone 6/6s/7的边界,您可以使用下一个比较:
1 2 3 | if KRDeviceType() == .iPhone6 { // Perform appropiate operations } |
要检测设备是否有iPhone 5/5s/SE或更早版本(iPhone 4S)的界限,可以使用下一个比较:
1 2 3 | if KRDeviceType() <= .iPhone5 { //iPhone 5/5s/SE of iPhone 4s // Perform appropiate operations (for example, set up constraints for those old devices) } |
在这么多层面上,依赖规模是错误的。我们向系统询问怎么样?
1 2 3 4 5 6 | - (NSString *) getDeviceModel { struct utsname systemInfo; uname(&systemInfo); return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; } |
从检测硬件类型的最佳方法,iphone4或iphone5?,Edzio27回答。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CGFloat height = [UIScreen mainScreen].bounds.size.height; NSLog(@"screen soze is %f",height); if (height>550) { // 4" screen-do some thing } else if (height<500) { // 3.5" screen- do some thing } |
如果项目是使用xcode 6创建的,那么使用下面提到的代码来检测设备。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | printf(" Detected Resolution : %d x %d ",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height); if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) { if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){ printf("Device Type : iPhone 4,4s"); }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136){ printf("Device Type : iPhone 5,5S/iPod 5"); }else if([[UIScreen mainScreen] nativeBounds].size.height == 1334){ printf("Device Type : iPhone 6"); }else if([[UIScreen mainScreen] nativeBounds].size.height == 2208){ printf("Device Type : iPhone 6+"); } } }else{ printf("Device Type : iPad"); } |
如果项目是在xcode 5中创建并在xcode 6中打开的,则使用下面提到的代码检测设备。(如果没有为iPhone 6.6+分配启动图像,则此代码有效)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | printf(" Detected Resolution : %d x %d ",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height); if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) { if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){ printf("Device Type : iPhone 4,4s"); appType=1; }else if([[UIScreen mainScreen] nativeBounds].size.height == 1136 || [[UIScreen mainScreen] nativeBounds].size.height == 1704){ printf("Device Type : iPhone 5,5S,6,6S/iPod 5"); appType=3; } } }else{ printf("Device Type : iPad"); appType=2; } |
如果您仍然同时使用Xcode 5,请使用以下代码检测设备(iPhone 6和6+将不会被检测到)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | printf(" Detected Resolution : %d x %d ",(int)[[UIScreen mainScreen] bounds].size.width,(int)[[UIScreen mainScreen] bounds].size.height); if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){ if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) { CGSize result = [[UIScreen mainScreen] bounds].size; CGFloat scale = [UIScreen mainScreen].scale; result = CGSizeMake(result.width * scale, result.height * scale); if(result.height == 960 || result.height == 480){ printf("Device Type : iPhone 4,4S"); }else if(result.height == 1136){ printf("Device Type : iPhone 5s/iPod 5"); } } }else{ printf("Device Type : iPad"); } |
添加"新swift文件"->
添加对
1 2 3 4 5 6 7 8 9 10 11 12 | import UIKit extension AppDelegate { class func isIPhone5 () -> Bool{ return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 568.0 } class func isIPhone6 () -> Bool { return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 667.0 } class func isIPhone6Plus () -> Bool { return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 736.0 } } |
用途:
1 2 3 4 5 | if AppDelegate.isIPhone5() { collectionViewTopConstraint.constant = 2 }else if AppDelegate.isIPhone6() { collectionViewTopConstraint.constant = 20 } |
这个问题已经回答了一百次,但这个解决方案对我来说效果最好。它是一个简单的助手函数,不需要扩展系统类。
斯威夫特3助手:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | func phoneSizeInInches(defaultValue: Float = 4.7) -> Float { switch (UIScreen.main.nativeBounds.size.height) { case 960, 480: return 3.5 case 1136: return 4 case 1334: return 4.7 case 2208: return 5.5 default: return defaultValue } } |
这是因为很容易记住手机的英寸大小,例如"5.5英寸"或"4.7英寸"设备,但很难记住确切的像素大小。
1 2 3 | if phoneSizeInInches() == 4 { //do something with only 4 inch iPhones } |
这也为您提供了这样做的机会:
1 2 3 | if phoneSizeInInches() < 5.5 { //do something all iPhones smaller than the plus } |
"defaultvalue"确保如果Apple发布了新的设备大小,而您尚未更新应用程序,那么您的代码将始终返回到安全大小。
1 2 3 | if phoneSizeInInches(defaultValue: 4.7) == 4 { //if a new iPhone size is introduced, your code will default to behaving like a 4.7 inch iPhone } |
请注意,这是针对手机应用程序的,需要对通用应用程序进行一些更改。
这样就可以检测设备系列。
1 2 3 4 5 6 7 8 9 10 11 12 | #import <sys/utsname.h> NSString* deviceName() { struct utsname systemInformation; uname(&systemInformation); NSString *result = [NSString stringWithCString:systemInformation.machine encoding:NSUTF8StringEncoding]; return result; } #define isIPhone5 [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound #define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound |
使用以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | CGFloat screenScale = [[UIScreen mainScreen] scale]; CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale); if (screenSize.height==1136.000000) { // Here iPhone 5 View // Eg: Nextview~iPhone5.Xib } else { // Previous Phones // Eg : Nextview.xib } |
以下是设备的正确测试,不取决于方向
1 2 3 4 5 6 7 8 | - (BOOL)isIPhone5 { CGSize size = [[UIScreen mainScreen] bounds].size; if (MIN(size.width,size.height) == 320 && MAX(size.width,size.height == 568)) { return YES; } return NO; } |
用于检测所有版本的iPhone和iPad设备。
1 2 3 4 5 6 | #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0) #define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0) #define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0) #define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0) |