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?
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With