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