iPhoneX、iOS11適配「封裝」

1、iPhoneX屏幕尺寸相關變化

1.1 高度增加了145pt,變成812pt.

1.2 屏幕圓角顯示,注意至少留10pt邊距。

1.3 狀態欄高度由20pt變成44pt,留意這個距離就能避開“劉海”的尷尬,相應的導航欄以上變化64->88。

1.4 底部工具欄需要為home indicator留出34pt邊距。

1.5 物理分辨率為1125px * 2436px.

2、調用

<code>//ios11適配安全邊距(宏定義)
adjustsScrollViewInsets_NO(self.tableView, self);/<code>

3、OC宏定義

// iPhone X 宏定義 #define iPhoneX (SCREEN_WIDTH == 375.f && SCREEN_HEIGHT == 812.f ? YES : NO) // 適配iPhone X 狀態欄高度 #define MC_StatusBarHeight (iPhoneX ? 44.f : 20.f) // 適配iPhone X Tabbar高度 #define MC_TabbarHeight (iPhoneX ? (49.f+34.f) : 49.f) // 適配iPhone X Tabbar距離底部的距離 #define MC_TabbarSafeBottomMargin (iPhoneX ? 34.f : 0.f) // 適配iPhone X 導航欄高度 #define MC_NavHeight (iPhoneX ? 88.f : 64.f) //iOS11安全邊距適配,防止tableview偏移。automaticallyAdjustsScrollViewInsets = NO #define adjustsScrollViewInsets_NO(scrollView,vc)\ do { \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored "-Warc-performSelector-leaks"") \ if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\ [scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\ } else {\ vc.automaticallyAdjustsScrollViewInsets = NO;\ }\ _Pragma("clang diagnostic pop") \ } while (0) /* if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } */ 4、swift宏定義 func ios11_0orLater() -> Bool { if #available(iOS 11.0, *) { return true } else { return false } } // iPhone X 宏定義 func iPhoneX() -> Bool { if UIScreen.main.bounds.width == 375 && UIScreen.main.bounds.height == 812{ return true }else{ return false } } // 適配iPhone X 狀態欄高度 func MC_StatusBarHeight() -> CGFloat { return iPhoneX() ? 44.0 : 20.0 } // 適配iPhone X Tabbar高度 func MC_TabbarHeight() -> CGFloat { return iPhoneX() ? (44.0 + 34.0) : 39.0 } // 適配iPhone X Tabbar距離底部的距離 func MC_TabbarSafeBottomMargin() -> CGFloat { return iPhoneX() ? 34.0 : 0.0 } // 適配iPhone X 導航欄高度 func MC_NavHeight() -> CGFloat { return iPhoneX() ? 88.0 : 64.0 } //宏定義適配滾動視圖偏移 func contentInsetAdjus(scroller:UIScrollView){ if #available(iOS 11.0, *) { scroller.contentInsetAdjustmentBehavior = .never } else { scroller.parentController().automaticallyAdjustsScrollViewInsets = false } }

5、tableview和collectionView兩個封裝的方法,調用適配iOS11和iPhoneX(UIView的擴展類)

_tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, MC_NavHeight, SCREEN_WIDTH, SCREEN_HEIGHT-MC_NavHeight) style:UITableViewStyleGrouped]; //iOS11、iPhoneX適配 [UIView tablevieiOS11:_tableView isHaveTabbar:NO]; /** tableviewIOS11適配,明傑刷新跳動和組頭組腳有空白 @param tableView 滾動視圖 @param ishaveTabbar 底部是否有工具條,有工具條傳入YES,沒有傳入NO */ +(void)tablevieiOS11:(UITableView*)tableView isHaveTabbar:(BOOL)ishaveTabbar{ #pragma mark =====繼承:XMRootViewController用【系統導航欄的】的 iOS11 tableview偏移適配(放到tableview初始化裡面)S============== if (@available(iOS 11.0, *)) { //1、tableView的section之間間距變大問題,解決辦法:初始化的時候增加以下代碼 //tableView 頭部視圖和尾部視圖出現一塊留白問題 //iOS11下tableview默認開啟了self-Sizing,Headers, footers, and cells都默認開啟Self-Sizing,所有estimated 高度默認值從iOS11之前的 0 改變為 tableView.estimatedRowHeight =0; tableView.estimatedSectionHeaderHeight =0; tableView.estimatedSectionFooterHeight =0; //2、MJ刷新異常,上拉加載出現跳動刷新問題,解決辦法:初始化的時候增加以下代碼(tableView和collectionView類似) tableView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever; if (ishaveTabbar==YES) { //底部有工具條 tableView.contentInset =UIEdgeInsetsMake(0,0, 0, 0);//底部有tabbar或者工具條的不改變偏移 }else{ //底部無工具條 tableView.contentInset =UIEdgeInsetsMake(0,0, MC_TabbarSafeBottomMargin, 0);//距離底部的距離,防止拉到最後被蓋住 } tableView.scrollIndicatorInsets =tableView.contentInset; } #pragma mark ======== iOS11 tableview偏移適配 E============== } /** collectionViewIOS11適配,明傑刷新跳動和組頭組腳有空白 @param collectionView 滾動視圖 @param ishaveTabbar 底部是否有工具條,有工具條傳入YES,沒有傳入NO */ +(void)collectionViewiOS11:(UICollectionView *)collectionView isHaveTabbar:(BOOL)ishaveTabbar{ #pragma mark =====繼承:XMRootViewController用【系統導航欄的】的 iOS11 tableview偏移適配(放到tableview初始化裡面)S============== if (@available(iOS 11.0, *)) { //2、MJ刷新異常,上拉加載出現跳動刷新問題,解決辦法:初始化的時候增加以下代碼(tableView和collectionView類似) collectionView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever; if (ishaveTabbar==YES) { //底部有工具條 collectionView.contentInset =UIEdgeInsetsMake(0,0, 0, 0);//底部有tabbar或者工具條的不改變偏移 }else{ //底部無工具條 collectionView.contentInset =UIEdgeInsetsMake(0,0, MC_TabbarSafeBottomMargin, 0);//距離底部的距離,防止拉到最後被蓋住 } collectionView.scrollIndicatorInsets =collectionView.contentInset; } #pragma mark ======== iOS11 tableview偏移適配 E============== }