在寫App的引導界面的時候遇到了這個問題,其實很早以前就遇到了這個問題,當時就是只是用一句代碼就可以去搞定。
if #available(iOS 11.0, *) {
scrollView.contentInsetAdjustmentBehavior = .never
}
在UIViewController中有一個屬性
@available(iOS, introduced: 7.0, deprecated: 11.0, message: "Use UIScrollView's contentInsetAdjustmentBehavior instead")
open var automaticallyAdjustsScrollViewInsets: Bool // Defaults to YES
此屬性的默認值為true,這使得當有滾動視圖插入,UIViewController會去考慮狀態(tài)欄、搜索欄、導航欄、工具欄或標簽欄所消耗的屏幕區(qū)域。如果UIViewController自己管理滾動視圖的插入調整,則將此屬性設置為false。
而在iOS11中UIViewController的這個屬性已經被廢除,轉而代替它的是的ScrollView的兩個屬性:
open var adjustedContentInset: UIEdgeInsets { get }
open var contentInsetAdjustmentBehavior: UIScrollViewContentInsetAdjustmentBehavior
首先來看看安全區(qū)域safeAreaInsets,官方文檔解釋如下:

安全區(qū)域就是不被狀態(tài)欄、搜索欄、導航欄、工具欄或標簽欄所消耗的屏幕區(qū)域所覆蓋。safeAreaInsets這個屬性其實指的就是一個view距離該view的安全區(qū)域的邊距。如果一個view全部在它父視圖的安全區(qū)域內,則SafeAreaInsets值為(0,0,0,0)。如果多了一個信號欄,那SafeAreaInset值為(20,0,0,0)。
var adjustedContentInset: UIEdgeInsets { get }
這是我們前面提到過的ScrollView的一個屬性,使用此屬性獲得在其中繪制內容的調整區(qū)域。簡單來說就是我們ScrollerView中呈現的內容需要上下左右調整多少會由這個屬性來進行控制。iOS11以前ScrollView內容與View邊緣距離是由contentInset來決定的而在iOS11后使用的是adjustedContentInset(內容偏移量)。那緊接著我們來說一下這個屬性是如何進行計算的,這個時候就需要用到contentInsetAdjustmentBehavior這個枚舉屬性了。
@available(iOS 11.0, *)
public enum UIScrollViewContentInsetAdjustmentBehavior : Int {
case automatic // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = 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
}
automatic和scrollableAxes屬性依賴于controller 的automaticallyAdjustsScrollViewContentInset屬性為true
(1)automatic
adjustedContentInset = safeAreaInset + contentInset
(2)scrollableAxes
在可以滾動的方向上 adjustedContentInset = safeAreaInset + contentInset
在不可以滾動的方向上 adjustedContentInset = contentInset
(3)never
adjustedContentInset = contentInset
(4)always
adjustedContentInset = safeAreaInset + contentInset
iPhone X由于是劉海屏幕,安全區(qū)域的問題官方的文檔也說明的很詳細了,可以戳鏈接:https://developer.apple.com/design/human-interface-guidelines/ios/overview/iphone-x/
文章的最后再拋出一個問題:現在再來看看我最近遇到的iOS10的一個問題,隱藏了導航欄,然后進入頁面后讓WKWebView滿鋪整個屏幕,進入之后發(fā)現網頁的內容整體下降了一個信號欄的高度。
因為WKWebView是繼承于ScrollView的,前面提到過在iOS11之前都是由UIViewController的automaticallyAdjustsScrollViewInsets(默認是true)屬性來控制是否讓他自己去適應滾動視圖的插入,而我沒有對這個屬性進行修改,所以會自適應讓整個網頁內容往下移動了一個信號欄的高度。