如何检测iPhone X上的运行?我尝试了以下代码。
#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模拟器中运行此宏时,IS_IPHONE_X
为真。这个宏是否正确?
答案 0 :(得分:5)
根据Apple Human Interface Guidelines,iPhone X&#39 屏幕宽度= 375,屏幕高度= 812所以,我认为这似乎是正确的!
您可以编写类似
的宏 #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 :(得分:1)
据我所知,最后一行对你来说应该是一个混乱。但你是对的
#define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)
由于纵向模式下iPhone X的高度为2436px(812pts),横向模式下的宽度为 2436px(812pts)。
正如Lion建议的纵向模式,以下代码适用于两种模式。您只需要更改宏的最后一行
#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
答案 2 :(得分:0)
HIII,
希望这对你有所帮助。
目标C
#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)
您可以在上面的列表中添加更多内容
<强>夫特强>
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)
使用此宏,如:
if (IS_IPAD) {
// your code hear
}