適配iOS11 - UITableview UICollectionView MJRefresh下拉刷新錯亂

適配iOS11 - UITableview UICollectionView MJRefresh下拉刷新錯亂

 

最新iOS 11 & iPhone X適配方案傳送門:10分鐘適配 iOS11 & iPhoneX

發現問題

升級Xcode 9 + iOS 11後,發現本來沒問題的collectionView和tableView像是中了風同樣,頭部刷新UI出現了錯亂。
查閱發現 iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它git

關於 contentInsetAdjustmentBehaviorgithub

@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 是一個枚舉類型,值有如下幾種:swift

-automatic 和scrollableAxes同樣,scrollView會自動計算和適應頂部和底部的內邊距而且在scrollView 不可滾動時,也會設置內邊距.
-scrollableAxes 自動計算內邊距.
-never不計算內邊距
-always 根據safeAreaInsets 計算內邊距app

很顯然,咱們這裏要設置爲 neverless

開始適配

OC 中

if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);//導航欄若是使用系統原生半透明的,top設置爲64
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        }

swift 中

if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0)//導航欄若是使用系統原生半透明的,top設置爲64
            tableView.scrollIndicatorInsets = tableView.contentInset
        }

凡是Scrollview 及 Scrollview子類都會有適配問題,整個工程使用了無數Scrollview的你心理陰影面積必定不小,別擔憂,其實能夠一行代碼解決問題:ide

// AppDelegate 進行全局設置
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }

終於又迴歸原來的效果啦

11.gifspa

更多代碼可參考
https://github.com/XuYang8026/UniversalProjectcode

以上屬於臭碼農原創,如有雷同屬巧合,若有錯誤望指正,轉載請標明來源和做者。
by:臭碼農get

 



做者:臭碼農
連接:https://www.jianshu.com/p/a6e5cc20a008
來源:簡書
簡書著做權歸做者全部,任何形式的轉載都請聯繫做者得到受權並註明出處。it

相關文章
相關標籤/搜索