【轉(zhuǎn)】適配iOS11 - UITableview UICollectionView MJRefresh下拉刷新錯(cuò)亂

文章來源:http://www.cocoachina.com/cms/wap.php?action=article&id=20101

升級(jí)Xcode 9 + iOS 11后,發(fā)現(xiàn)原本沒問題的collectionView和tableView像是中了風(fēng)一樣,頭部刷新UI出現(xiàn)了錯(cuò)亂。

查閱發(fā)現(xiàn) iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它

關(guān)于 contentInsetAdjustmentBehavior

@available(iOS 11.0, *)
public enum UIScrollViewContentInsetAdjustmentBehavior : Int {
case automatic // Similar to .scrollableAxes, but will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewContentInset = YES inside a navigation controller, regardless of whether the scroll view is scrollable case scrollableAxes // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
case never // contentInset is not adjusted
case always // contentInset is always adjusted by the scroll view's safeAreaInsets
}

UIScrollViewContentInsetAdjustmentBehavior 是一個(gè)枚舉類型,值有以下幾種:

-automatic 和scrollableAxes一樣,scrollView會(huì)自動(dòng)計(jì)算和適應(yīng)頂部和底部的內(nèi)邊距并且在scrollView 不可滾動(dòng)時(shí),也會(huì)設(shè)置內(nèi)邊距.

-scrollableAxes 自動(dòng)計(jì)算內(nèi)邊距.
-never不計(jì)算內(nèi)邊距
-always 根據(jù)safeAreaInsets 計(jì)算內(nèi)邊距

很顯然,我們這里要設(shè)置為 never

開始適配

OC 中

        //聲明tableView的位置 添加下面代碼
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        }

swift 中

        //聲明tableView的位置 添加下面代碼
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0)
            tableView.scrollIndicatorInsets = tableView.contentInset
        }

終于又回歸原來的效果啦

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容