iOS刘海屏适配,iPhoneX、iPhone12系列导航栏高度,刘海平适配

NAVIHEIGHT:(iPhoneX ? 88 : 64)、 TABBARHEIGHT: (iPhoneX ? 83 : 49)

新的iPhone、iPhone12发布后,我*件想做的事就是如何适配新iPhone,新的iPhone都是刘海屏幕

先看下各个尺寸

%title插图%num
各版本iPhone的尺寸及分辨率

%title插图%num

于是我就做了下面的宏定义(ScreenHeight就省略了):

#define iPhoneX :(ScreenHeight == 812.0f || ScreenHeight == 896.0f || ScreenHeight == 844.0f || ScreenHeight == 926.0f)

#define AdaptNaviHeight      (iPhoneX ? 24 : 0) //状态栏高度

#define AdaptTabHeight       (iPhoneX ? 34 : 0) //Tab bar 圆角部分高度

#define NAVIHEIGHT           (iPhoneX ? 88 : 64) //导航

#define TABBARHEIGHT         (iPhoneX ? 83 : 49) // 分栏

除了上面的方法外,官方的iPhone型号对照已更新,我们可以根据对照表,找到具体的手机型号。

具体方法及枚举如下

+ (NSString *)getDeviceSystemName {
static dispatch_once_t one;
static NSString *name;
dispatch_once(&one, ^{
NSString *model = [[UIDevice currentDevice] machineModel];
if (!model) return;
NSDictionary *dic = @{
@”iPhone7,2″ : @”iPhone 6″,
@”iPhone7,1″ : @”iPhone 6 Plus”,
@”iPhone8,1″ : @”iPhone 6s”,
@”iPhone8,2″ : @”iPhone 6s Plus”,
@”iPhone8,4″ : @”iPhone SE”,
@”iPhone9,1″ : @”iPhone 7″,
@”iPhone9,2″ : @”iPhone 7 Plus”,
@”iPhone9,3″ : @”iPhone 7″,
@”iPhone9,4″ : @”iPhone 7 Plus”,
@”iPhone10,1″ : @”iPhone 8″,
@”iPhone10,4″ : @”iPhone 8″,
@”iPhone10,2″ : @”iPhone 8 Plus”,
@”iPhone10,5″ : @”iPhone 8 Plus”,
@”iPhone10,3″ : @”iPhone X”,
@”iPhone10,6″ : @”iPhone X”,
@”iPhone11,2″ : @”iPhone XS”,
@”iPhone11,4″ : @”iPhone XS Max”,
@”iPhone11,6″ : @”iPhone XS Max”,
@”iPhone11,8″ : @”iPhone XR”,
};
name = dic[model];
});
return name;

}
tips:

1、关于每个控制器的适配,我采用了继承的方式,并且自定义了导航栏,所以导航栏的高度我在基控制器就做了控制,子控制器直接取自定义的导航,就可以做页面适配了。

2、关于自定义导航上的控件,每个按钮和标题都以导航的底部为参考,这样导航高度变了,也不会导致导航上控件的位置错误。