因為項目在iOS11系統(tǒng)下運行時發(fā)現(xiàn)tableview和collectionView出現(xiàn)內(nèi)容自動向下偏移了20的高度路召,在查找問題的時候,發(fā)現(xiàn)了相關(guān)的新特性栋烤,從而解決了適配問題谒养。
iOS11系統(tǒng)下添加了一些新特性,具體內(nèi)容可仔細閱讀這篇文章明郭,本文涉及內(nèi)容是UIScrollView and UITableView的新特性
這里只介紹怎么解決問題
新增的contentInsetAdjustmentBehavior屬性用來配置adjustedContentInset的行為买窟,該結(jié)構(gòu)體有以下幾種類型:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
// 會自動計算和適應(yīng)頂部和底部的內(nèi)邊距并且在scrollView 不可滾動時,也會設(shè)置內(nèi)邊距.
UIScrollViewContentInsetAdjustmentAutomatic,
// 自動計算內(nèi)邊距
UIScrollViewContentInsetAdjustmentScrollableAxes,
// 不計算內(nèi)邊距
UIScrollViewContentInsetAdjustmentNever,
// 根據(jù)safeAreaInsets 計算內(nèi)邊距
UIScrollViewContentInsetAdjustmentAlways,
}
顯然,我們要使用UIScrollViewContentInsetAdjustmentNever
先要判斷版本薯定,避免不能識別代碼始绍,設(shè)置相關(guān)的UIScrollView的子類的contentInsetAdjustmentBehavior
屬性
最終答案就是下面這句代碼
#ifdef __IPHONE_11_0
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
self.collection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif