一、几个适配iPhoneX有用的宏/函数
1、宏
//判断iOS11
#define iOS11OrLater @available(iOS 11.0, *)
//判断iPhoneX
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_X (IS_IPHONE && SCREEN_WIDTH == 375.0f && SCREEN_HEIGHT == 812.0f)
2、函数
//获取视图底部极限<适用于Masonry实现页面布局>
MASViewAttribute* MD_SafeAreaInset_Masonry_Boom(UIView * view){
if(iOS11OrLater)
{
return view.mas_safeAreaLayoutGuideBottom;
}
return view.mas_bottom;
}
//获取视图顶部极限<适用于Masonry实现页面布局>
MASViewAttribute* MD_SafeAreaInset_Masonry_Top(UIView * view){
if(iOS11OrLater)
{
return view.mas_safeAreaLayoutGuideTop;
}
return view.mas_top;
}
//获取视图安全区域<适用于非Masonry实现页面布局>
UIEdgeInsets MD_SafeAreaInset(UIView * view){
if(iOS11OrLater)
{
if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, view.safeAreaInsets))
{
return view.safeAreaInsets;
}
}
//防止获取SafeAreaInset获取zero
if(IS_IPHONE_X)
{
return UIEdgeInsetsMake(44, 0, 34, 0);
}
return UIEdgeInsetsZero;
}
iOS11适配
1、scrollview布局及tableview显示/抖动适配
if (iOS11OrLater)
{
[UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
[UITableView appearance].estimatedSectionFooterHeight = 0;
[UITableView appearance].estimatedSectionHeaderHeight = 0;
[UITableView appearance].estimatedRowHeight = 0;
}
2、webview适配(cell中包含webview若不处理,最终webview内容将被遮挡部分高度)
if(iOS11OrLater)
{
self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}