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

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

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

查閱發(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 是一個枚舉類型,值有以下幾種:

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

-scrollableAxes 自動計算內(nèi)邊距.
-never不計算內(nèi)邊距
-always 根據(jù)safeAreaInsets 計算內(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)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容