Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect iPhone X with a macro [duplicate]

How can I detect running on an iPhone X? I tried the following code.

#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)

When I run this macro in the iOS simulator, then IS_IPHONE_X is true. Is this macro correct?

like image 909
Kosuke Ogawa Avatar asked Sep 13 '17 11:09

Kosuke Ogawa


2 Answers

According to the Apple Human Interface Guidelines, iPhone X's screen width = 375 and screen height = 812 so, it seems correct I think!

You can write macros something like,

 #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)
like image 175
Ketan Parmar Avatar answered Nov 08 '22 19:11

Ketan Parmar


The last line, as far as I know should be a confusion for you. But you are correct

#define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)

Since height for iPhone X in portrait mode is 2436px(812pts) and width in landscape mode is 2436px (812pts).

As Lion suggested for Portrait mode, the below code is for both the modes. You just have to change your lastline of your macro

    #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
like image 1
Aditya Srivastava Avatar answered Nov 08 '22 20:11

Aditya Srivastava