现在Apple市场上,越来越流行刘海屏手机(与Android市场一样,往刘海屏手机方向发展趋势)。
在iPhone手机角度上看,刘海屏出现在机型较新的手机上(相对较旧/老的iPhone上还没出现)。
那么,如何判断当前的iPhone手机是刘海屏手机呢?同时,如何获取刘海高度?
在这里,提供一个工具类:
NotchScreenUtil.h
/*
* iPhone刘海屏工具类
*/
@interface NotchScreenUtil : NSObject
// 判断是否是刘海屏
+(BOOL)isIPhoneNotchScreen;
// 获取刘海屏高度
+(CGFloat)getIPhoneNotchScreenHeight;
@end
NotchScreenUtil.m
#import
#import "NotchScreenUtil.h"
@implementation NotchScreenUtil
+ (BOOL)isIPhoneNotchScreen{
BOOL result = NO;
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
return result;
}
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.bottom > 0.0) {
result = YES;
}
}
return result;
}
+ (CGFloat)getIPhoneNotchScreenHeight{
/*
* iPhone8 Plus UIEdgeInsets: {20, 0, 0, 0}
* iPhone8 UIEdgeInsets: {20, 0, 0, 0}
* iPhone XR UIEdgeInsets: {44, 0, 34, 0}
* iPhone XS UIEdgeInsets: {44, 0, 34, 0}
* iPhone XS Max UIEdgeInsets: {44, 0, 34, 0}
*/
CGFloat bottomSpace = 0;
if (@available(iOS 11.0, *)) {
UIEdgeInsets safeAreaInsets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;
switch (UIApplication.sharedApplication.statusBarOrientation) {
case UIInterfaceOrientationPortrait:
{
bottomSpace = safeAreaInsets.bottom;
}
break;
case UIInterfaceOrientationLandscapeLeft:
{
bottomSpace = safeAreaInsets.right;
}
break;
case UIInterfaceOrientationLandscapeRight:
{
bottomSpace = safeAreaInsets.left;
}
break;
case UIInterfaceOrientationPortraitUpsideDown:
{
bottomSpace = safeAreaInsets.top;
}
break;
default:
{
bottomSpace = safeAreaInsets.bottom;
}
break;
}
}
return bottomSpace;
}
@end
注:开放接口可用范围在iOS 11以上。
本文由 @程序员小新[Vip] 发布于 职涯宝 ,未经作者许可,禁止转载,欢迎您分享文章