Detecting iPhone X with Macro

How can I detect startup on 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 an iOS simulator, then IS_IPHONE_Xtrue. Is this macro correct?

+4
source share
3 answers

According to the Apple Human Interface Guide , the iPhone X has a screen width = 375 and a screen height = 812, so it seems right, I think!

You can write macros 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)
+5
source

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

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

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

, .

    #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
+1

Hiii,

Hope this helps you.

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

you can add one more list above

Swift

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)

use this macro like:

if (IS_IPAD) {
        // your code hear
}
0
source

Source: https://habr.com/ru/post/1685593/


All Articles