升級Xcode 9 + iOS 11後,發現本來沒問題的collectionView和tableView像是中了風同樣,頭部刷新UI出現了錯亂。
查閱發現 iOS11棄用了automaticallyAdjustsScrollViewInsets
屬性,新增contentInsetAdjustmentBehavior
來替代它git
關於 contentInsetAdjustmentBehavior
github
@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
很顯然,咱們這裏要設置爲 never
less
if (@available(iOS 11.0, *)){ _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);//導航欄若是使用系統原生半透明的,top設置爲64 _tableView.scrollIndicatorInsets = _tableView.contentInset; }
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