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

升級Xcode 9 + iOS 11後,發現原本沒問題的collectionView和tableView像是中了風一樣,頭部刷新UI出現了錯亂。

查閱發現 iOS11棄用了automaticallyAdjustsScrollViewInsets屬性,新增contentInsetAdjustmentBehavior來替代它

關於 contentInsetAdjustmentBehavior

1
2
3
4
5
6
7
8
9
10
11
@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會自動計算和適應頂部和底部的內邊距並且在scrollView 不可滾動時,也會設置內邊距.

  • -scrollableAxes 自動計算內邊距.

  • -never不計算內邊距

  • -always 根據safeAreaInsets 計算內邊距

很顯然,我們這裏要設置爲 never

開始適配

OC 中

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

swift 中

1
2
3
4
5
6
        //聲明tableView的位置 添加下面代碼
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0)
            tableView.scrollIndicatorInsets = tableView.contentInset
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章