iOS11之后新增了一个safeAreaInsets属性
可以利用 CGFloat a = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;
当a大于0时即是iPhone X ,XR,XS ,XS Max等。
if (@available(iOS 11.0, *)) { CGFloat a = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom; NSLog(@"%f",a); } else { // Fallback on earlier versions }
#define IPHONE_X ({BOOL isPhoneX = NO; if (@available(iOS 11.0, *)) { isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0; } (isPhoneX);}) #define isIphoneX ({ BOOL isPhoneX = NO; if (@available(iOS 11.0, *)) { if (!UIEdgeInsetsEqualToEdgeInsets([UIApplication sharedApplication].delegate.window.safeAreaInsets, UIEdgeInsetsZero)) { isPhoneX = YES; } } isPhoneX; })