Detect iPhone X with a macro
本问题已经有最佳答案,请猛点这里访问。
如何检测在iPhone X上运行?我尝试了以下代码。
1 2 3 4 5 | #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #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 IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0) |
当我在iOS模拟器中运行这个宏时,
根据苹果人机界面指南,iPhone X屏幕宽度=375,屏幕高度=812,所以,我认为这似乎是正确的!
你可以编写宏,比如,
1 2 3 4 5 6 7 8 9 | #define IS_IPHONE4 (([[UIScreen mainScreen] bounds].size.height-480)?NO:YES) #define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) #define IS_IPHONE6 (([[UIScreen mainScreen] bounds].size.height-667)?NO:YES) #define IS_IPHONE6P (([[UIScreen mainScreen] bounds].size.height-736)?NO:YES) #define IS_IPHONEX (([[UIScreen mainScreen] bounds].size.height-812)?NO:YES) |
最后一句话,据我所知,对你来说应该是一种困惑。但是你是对的
1 | #define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0) |
因为iPhoneX在纵向模式下的高度是2436px(812pts),而横向模式下的宽度是2436px(812pts)。
正如Lion建议的肖像模式,下面的代码适用于这两种模式。只需更改宏的最后一行
1 2 3 4 5 6 7 8 9 10 | #define IS_IPHONE_4 (IS_IPHONE && SCREEN_MAX_LENGTH == 480.0) //iphone 4 #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) //iphone 5 #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) //iphone 6 #define IS_IPHONE_6p (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0) //iphone6p #define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0) //iphone x |
希伊
希望这对你有帮助。
目标C
1 2 3 4 5 6 7 8 | #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_IPHONE_4S (IS_IPHONE && ([[UIScreen mainScreen] bounds].size.height == 480.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER)) #define IS_IPHONE_5 (IS_IPHONE && ([[UIScreen mainScreen] bounds].size.height == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER)) #define IS_STANDARD_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) #define IS_ZOOMED_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale) #define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0) #define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale) |
您可以向以上列表添加更多内容
迅捷
1 2 3 4 5 6 7 | let IS_IPAD = (UI_USER_INTERFACE_IDIOM() == .pad) let IS_IPHONE = (UI_USER_INTERFACE_IDIOM() == .phone) let IS_IPHONE_4S = (IS_IPHONE && (UIScreen.main.bounds.size.height == 480.0) && ((IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale) || !IS_OS_8_OR_LATER)) let IS_IPHONE_5 = (IS_IPHONE && (UIScreen.main.bounds.size.height == 568.0) && ((IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale) || !IS_OS_8_OR_LATER)) let IS_STANDARD_IPHONE_6 = (IS_IPHONE && UIScreen.main.bounds.size.height == 667.0 && IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale) let IS_ZOOMED_IPHONE_6 = (IS_IPHONE && UIScreen.main.bounds.size.height == 568.0 && IS_OS_8_OR_LATER && UIScreen.main.nativeScale > UIScreen.main.scale) let IS_STANDARD_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.main.bounds.size.height == 736.0) |
使用如下宏:
1 2 3 | if (IS_IPAD) { // your code hear } |