一惫撰、簡述
在iOS11以后的系統(tǒng)中UIViewController
的automaticallyAdjustsScrollViewInsets
這個屬性過期棄用了不再建議使用了凉翻。系統(tǒng)推薦我們使用UIScrollView
的contentInsetAdjustmentBehavior
屬性替代它囊卜。如果項目之前使用了self. automaticallyAdjustsScrollViewInsets = YES
,并且設置scrollView.contentInset
的值,此時升級到iOS11以上的系統(tǒng)時頁面就會出現(xiàn)布局問題籍嘹。下面簡單的寫下關于contentInsetAdjustmentBehavior
這個屬性的理解。關于這個屬性提供了四種設置值弯院。
備注:當
UIScrollView
的frame
超出安全區(qū)域時辱士,contentInsetAdjustmentBehavior
決定了adjustContentInset
的計算方式是否會受到safeAreaInset
安全區(qū)域值的影響,進而影響了adjustContentInset
的最終值听绳。最終影響到UIScrollView
的content的內(nèi)邊距颂碘。
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic,
UIScrollViewContentInsetAdjustmentScrollableAxes,
UIScrollViewContentInsetAdjustmentNever,
UIScrollViewContentInsetAdjustmentAlways,
}
二、UIScrollViewContentInsetAdjustmentBehavior 和 adjustContentInset
1. UIScrollViewContentInsetAdjustmentAutomatic
默認值椅挣,scrollView會根據(jù)頁面的安全區(qū)域的值自動調(diào)整計算头岔,系統(tǒng)在計算content的edgeInset
時會考慮到安全區(qū)域自動計算和適應頂部和底部的內(nèi)邊距塔拳。即使UIScrollView
不可滾動,也會自動設置content的內(nèi)邊距峡竣。其他情況下行為與UIScrollViewContentInsetAdjustmentScrollableAxes
相同蝙斜。
- 如何計算adjustContentInset值:
adjustContentInset = safeAreaInset + contentInset
2. UIScrollViewContentInsetAdjustmentScrollableAxes
也是自動調(diào)整計算,但是會考慮滾動方向澎胡,系統(tǒng)會根據(jù)UIScrollView
的滾動方向進行判斷內(nèi)容的內(nèi)邊距是否要考慮安全區(qū)域孕荠,依賴于scrollEnabled和alwaysBounceHorizontal / vertical = YES。
eg. 如果是一個橫向滾動的UIScrollView
攻谁,及便布局起點和高度值超過了頁面的安全區(qū)稚伍,那么系統(tǒng)也不會調(diào)整。
- 如何計算adjustContentInset值:
可滾動方向:
adjustContentInset = safeAreaInset + contentInset
不可滾動方向:
adjustContentInset = contentInset
3. UIScrollViewContentInsetAdjustmentNever
內(nèi)容的內(nèi)邊距從不考慮安全區(qū)域戚宦,當contentInsetAdjustmentBehavior
設置為Never
的時候个曙,adjustContentInset
值不受SafeAreaInset
值的影響。
eg. 就算UIScrollView
超出了safeAreaInsets
受楼,系統(tǒng)不會對你的scrollView.adjustedContentInse
t做任何事情垦搬,即不作任何調(diào)整。
- 如何計算adjustContentInset值:
adjustContentInset = contentInset
4. UIScrollViewContentInsetAdjustmentAlways
內(nèi)容的內(nèi)邊距總是考慮安全區(qū)域艳汽,只要UIScrollView
的frame
超出安全區(qū)域就調(diào)整相應top&bottom的超出值猴贰,調(diào)整的最大值不會超過安全區(qū)相應方向的距離的最大值。
- 如何計算adjustContentInset值:
adjustContentInset = safeAreaInset + contentInset
三河狐、測試
-
測試一: 默認設置
contentInsetAdjustmentBehavior
為Automatic
米绕,頁面延伸布局為整個屏幕,從屏幕頂端開始計算馋艺;
圖1
由圖1可見栅干,在默認配置下UITableView
的內(nèi)容顯示正常,其內(nèi)容的內(nèi)邊距計算考慮到了安全距離捐祠。
view.safeAreaInsets = {88, 0, 83, 0}
tableView.contentInset = {0, 0, 0, 0}
tableView.adjustedContentInset = {88, 0, 83, 0}
-
測試二: 默認設置
contentInsetAdjustmentBehavior
為Automatic
碱鳞,頁面延伸布局為整個屏幕,從屏幕頂端開始計算踱蛀,并且設置UITableView
的contentInset
的值為UIEdgeInsetsMake(40, 0, 0, 0)
窿给;
圖2
由圖2可見,在此配置下UITableView
的內(nèi)容顯示不正常星岗,UITableView
的內(nèi)容的內(nèi)邊距向下偏移了40
view.safeAreaInsets = {88, 0, 83, 0}
tableView.contentInset = {40, 0, 0, 0}
tableView.adjustedContentInset = {128, 0, 83, 0}
-
測試三: 默認設置
contentInsetAdjustmentBehavior
為Automatic
填大,頁面延伸布局為整個屏幕戒洼,從屏幕頂端開始計算俏橘,并且設置UITableView
的frame的y值為50;
view.safeAreaInsets = {88, 0, 83, 0}
tableView.contentInset = {0, 0, 0, 0}
tableView.adjustedContentInset = {38, 0, 83, 0}
-
測試四: 默認設置
contentInsetAdjustmentBehavior
為Automatic
圈浇,頁面延伸布局為整個屏幕寥掐,從屏幕頂端開始計算靴寂,并且設置UITableView
的frame的y值為-50;
view.safeAreaInsets = {88, 0, 83, 0}
tableView.contentInset = {0, 0, 0, 0}
tableView.adjustedContentInset = {88, 0, 83, 0}
由圖3可見召耘,在UITableView
的頂部偏移父view的頂端50像素百炬,下移,在此配置下UITableView
的內(nèi)容顯示正常污它,并且調(diào)整值小于安全區(qū)域垂直方向的最大值;
由圖4可見剖踊,在UITableView
的頂部偏移父view的頂端-50像素,上移衫贬,在此配置下UITableView
的內(nèi)容顯示不正常德澈,并且調(diào)整值為安全區(qū)域垂直方向的最大值;
結合圖3和圖4,可證明調(diào)整的最大值不會超過安全區(qū)相應方向的距離的最大值固惯。
-
測試五: 設置
contentInsetAdjustmentBehavior
為Never
梆造,頁面延伸布局為整個屏幕,從屏幕頂端開始計算葬毫;
view.safeAreaInsets = {88, 0, 83, 0}
tableView.contentInset = {0, 0, 0, 0}
tableView.adjustedContentInset = {0, 0, 0, 0}
由圖5可見镇辉,在設置為Never
時,UITableView
的內(nèi)容內(nèi)邊距不會再考慮安全區(qū)域贴捡,因此系統(tǒng)計算adjustedContentInset
的調(diào)整至為0忽肛,頁面布局顯示異常,此時adjustedContentInset = contentInset烂斋。