iOS 11中Controller的automaticallyAdjustsScrollViewInsets屬性被廢棄了.
替代的是上文說的UIScrollerView中的contentInsetAdjustmentBehavior.
這個屬性是枚舉類型:
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic,
UIScrollViewContentInsetAdjustmentScrollableAxes,
UIScrollViewContentInsetAdjustmentNever,
UIScrollViewContentInsetAdjustmentAlways,
} API_AVAILABLE(ios(11.0),tvos(11.0));
如何理解這幾個枚舉類型的區(qū)別和意思需要先弄明白下面幾個屬性.
safeAreaInset
先說一下什么是安全區(qū)域,安全區(qū)域幫助我們將view放置在整個屏幕的可視的部分赃份。即使把navigationbar設(shè)置為透明的锭弊,系統(tǒng)也認(rèn)為安全區(qū)域是從navigationbar的bottom開始秉版,保證不被系統(tǒng)的狀態(tài)欄删咱、或?qū)Ш綑诟采w。
safeAreaInsets屬性反映了一個view距離該view的安全區(qū)域的邊距。換一句更容易理解的是:去掉StatusBar身害、NavigationBar狞贱、TabBar等視圖,距離屏幕上下左右的距離. safeAreaInsets并不是一個需要設(shè)置的屬性,系統(tǒng)會根據(jù)各種Bar的情況自己計算(包括在iphoneX上下的距離計算).
contentInset
contentInset在上一篇文章中介紹過了,表示一個ScrollerView的內(nèi)容周圍增加的邊距,這個系統(tǒng)默認(rèn)是0,需要手動的去調(diào)整設(shè)置.
adjustedContentInset
自適應(yīng)內(nèi)容邊距, contentInsetAdjustmentBehavior的各種值設(shè)置的就是這個屬性.
1.UIScrollViewContentInsetAdjustmentAutomatic:
如果scrollview在一個automaticallyAdjustsScrollViewContentInset = YES的controller上垮斯,并且這個Controller包含在一個navigationController中郎仆,這種情況下會設(shè)置在top & bottom上 adjustedContentInset = safeAreaInset + contentInset(不管是否滾動)。其他情況下與UIScrollViewContentInsetAdjustmentScrollableAxes相同.
2.UIScrollViewContentInsetAdjustmentScrollableAxes:
在可滾動方向上adjustedContentInset = safeAreaInset + contentInset兜蠕,在不可滾動方向上adjustedContentInset = contentInset扰肌;依賴于scrollEnabled和alwaysBounceHorizontal / vertical = YES,scrollEnabled默認(rèn)為yes熊杨,所以大多數(shù)情況下曙旭,計算方式還是adjustedContentInset = safeAreaInset + contentInset.
3.UIScrollViewContentInsetAdjustmentNever:
adjustedContentInset = contentInset
4.UIScrollViewContentInsetAdjustmentAlways:
adjustedContentInset = safeAreaInset + contentInset
addtionalSafeAreaInset
通過此設(shè)置可以修改safeAreaInset,進(jìn)而影響內(nèi)容視圖的位置.