1.判斷屏幕大小
第一種:
#define isiPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
第二種:
#define isiPhoneX?(CGSizeEqualToSize(CGSizeMake(375.f, 812.f), [UIScreen mainScreen].bounds.size) || CGSizeEqualToSize(CGSizeMake(812.f, 375.f), [UIScreen mainScreen].bounds.size))
第三種:
#define isiPhoneX?([UIScreen mainScreen].bounds.size.height == 812.0f || [UIScreen mainScreen].bounds.size.height == 896.0f)
第四種:(推薦)
//iPhone X 底部安全距離
#define LJ_SafeBottomHeight [LJUserManager safeBottomHeight]
//是否iPhone X
#define LJ_iPhoneX ? [LJUserManager isiPhoneX]
//判斷是否為iphone X
+ (BOOL)isiPhoneX {
? ? if(@available(iOS11.0, *)) {
? ? ? ? UIWindow*keyWindow = [[[UIApplicationsharedApplication]delegate]window];
? ? ? ? CGFloatbottomSafeInset = keyWindow.safeAreaInsets.bottom;
? ? ? ? if(bottomSafeInset ==34.0f|| bottomSafeInset ==21.0f) {
? ? ? ? ? ? returnYES;
? ? ? ? }
? ? }
? ? return NO;
}
//安全區(qū)域高度
+ (float)safeBottomHeight {
? ? if(@available(iOS11.0, *)) {
? ? ? ? return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.bottom;
? ? }
? ? return 0.0f;
}
2.判斷UIWindow的safeAreaInsets适刀,當(dāng)返回值為0時(shí)廓脆,為長(zhǎng)方形拗馒,非0時(shí)即認(rèn)為是iphone x涣觉,原理是判斷是傳統(tǒng)長(zhǎng)方形還是圓角矩形犁苏。
Swift:
func isIPhoneXType() -> Bool {
?????guard #available(iOS 11.0, *) else {
?????????return false
?????}
?????return UIApplication.shared.windows[0].safeAreaInsets != UIEdgeInsets.zero
}OC:
static inline BOOL isIPhoneXSeries() {
?????BOOL iPhoneXSeries = NO;
?????if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
?????????return iPhoneXSeries;
?????}
?????if (@available(iOS 11.0, *)) {
?????????UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];? ? ? ? ? ?if (mainWindow.safeAreaInsets.bottom > 0.0) {
?????????????iPhoneXSeries = YES;
?????????}
?????}
?????return iPhoneXSeries;
}?