美文网首页iOS趣味开发
IOS 判断为iPhone X的几种方法

IOS 判断为iPhone X的几种方法

作者: 孟华007 | 来源:发表于2018-10-12 17:29 被阅读279次

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;

}

//安全区域高度

+ (float)safeBottomHeight {

    if(@available(iOS11.0, *)) {

        return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.bottom;

    }

    return 0.0f;

}

2.判断UIWindow的safeAreaInsets,当返回值为0时,为长方形,非0时即认为是iphone x,原理是判断是传统长方形还是圆角矩形。
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;

相关文章

网友评论

    本文标题:IOS 判断为iPhone X的几种方法

    本文链接:https://www.haomeiwen.com/subject/qxvgaftx.html