1. 程式人生 > >適配iOS 11及iPhoneX iOS 11下tableView內容下移的問題

適配iOS 11及iPhoneX iOS 11下tableView內容下移的問題

一.為什麼會發生內容下移

1.原因分析

在iOS 11中Apple幹掉了ViewController中的automaticallyAdjustsScrollViewInsets這個屬性,當tableview的frame超出了安全區域後系統會自動的調整SafeAreaInsets的值,而iOS 11中真正影響tableview內容與邊緣的變成了adjustedContentInset而不是以前的contentInset。由於系統對adjustedContentInset進行了調整導致了tableView的內容到邊緣的距離發生了變化,下移距離分別是20pt(沒有navigationBar,下移了一個statusBar的高度),64pt(navigationBar的高度以及statusBar的高度)。

2.關於安全區域

安全區域的概念是在iOS 11提出的,如圖

簡單來說下什麼是安全區域,就是把View放在整個螢幕的可視部分,當有navigationbar存在時安全區域也是從navigationbar的bottom開始的,若同時存在tabBar則中間區域為安全區域,ViewController中還提供了additionalSafeAreaInsets去擴充套件安全區域。

safeAreaInsets屬性反映了一個view距離該view的安全區域的邊距。對於一個Controller的根檢視而言,SafeAreaInsets值包括了被statusbar和其他可視的bars覆蓋的區域和其他通過additionalSafeAreaInsets自定義的insets值。對於view層次中得其他view,SafeAreaInsets值反映了view被覆蓋的部分。如果一個view全部在它父檢視的安全區域內,則SafeAreaInsets值為(0,0,0,0)。

二、 adjustContentInset

在iOS11中scrollView新增的兩個屬性:adjustContentInset和contentInsetAdjustmentBehavior。

adjustContentInset表示contentView.frame.origin偏移了scrollview.frame.origin多少;是系統計算得來的,計算方式由contentInsetAdjustmentBehavior決定。有以下幾種計算方式:

UIScrollViewContentInsetAdjustmentAutomatic:如果scrollview在一個automaticallyAdjustsScrollViewContentInset = YES的controller上,並且這個Controller包含在一個navigation controller中,這種情況下會設定在top & bottom上 adjustedContentInset = safeAreaInset + contentInset不管是否滾動。其他情況下與UIScrollViewContentInsetAdjustmentScrollableAxes相同

UIScrollViewContentInsetAdjustmentScrollableAxes: 在可滾動方向上adjustedContentInset = safeAreaInset + contentInset,在不可滾動方向上adjustedContentInset = contentInset;依賴於scrollEnabled和alwaysBounceHorizontal / vertical = YES,scrollEnabled預設為yes,所以大多數情況下,計算方式還是adjustedContentInset = safeAreaInset + contentInset

UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset

UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset

當contentInsetAdjustmentBehavior設定為UIScrollViewContentInsetAdjustmentNever的時候,adjustContentInset值不受SafeAreaInset值的影響。

三、tableView何時會發生偏移問題

最常見的場景:tableview的frame超出了安全區域,而且設定了tableview的contentInset,第一幅圖中的tableview的frame設定為(0,0,self.view.frame.size.width,self.view.frame.size.height),contentInset設定為UIEdgeInsetsMake(64,0,0,0);從而導致了adjustedContentInset 偏移了一個safeAreaInset + contentInset,簡單來說:當tableview的frame超出安全區域之後,系統會自動調整tableview的顯示範圍,但此時又設定了contentInset屬性導致出現下移現象。

四、解決方案

1.去掉contentInset

因為在iOS 11中系統已經預設對scrollview的顯示做了處理只要其超過安全區域,它的內容顯示都會在view上正常顯示,所以就不需要設定contentInset,避免發生偏移

2.設定contentInsetAdjustmentBehavior

在不改變contentInset的情況下通過設定tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;讓adjustContentInset值不受SafeAreaInset值的影響。

3. iOS 11 ViewController新增的屬性 addtionalSafeAreaInset;

在不改變contentInset的情況下通過增加安全區域的範圍來抵消掉SafeAreaInset的值,如果SafeAreaInset值為(20,0,0,0),那麼設定additionalSafeAreaInsets屬性值為(-20,0,0,0),則SafeAreaInsets不會對adjustedContentInset值產生影響

4.把tableview的frame控制在安全區域內且不設定contentInset

在我的專案中對基類控制器中添加了一個ContentView屬性,該View的區域範圍也就是安全區域的範圍,需要做的就是先算出View的高度,把tableview放到安全區域內就可以了,這樣也有個好處就是,iOS 11只對scrollview進行了安全區域的顯示處理,如果是view的話超出安全區域外的內容是無法顯示的。

這裡說一下automaticallyAdjustsScrollViewInsets這個屬性,在iOS 11之前即使把tableview放到了可視範圍之內(有NavigationBar且沒有設定contentInset),當該屬性為YES時候tableview還是會發生偏移。。。(很尷尬,所以這個屬性我一般至為NO,瞬間覺得iOS 11做了一件很美好的事情)

對於安全區域高度的計算有必要說一下,僅在豎屏有NavigationBar的情況下,傳統的iPhone尺寸安全區域高度為SCREEN_HEIGHT-64 ;而iPhoneX中為SCREEN_HEIGHT-(44+44+34),(第一個44為iPhoneX狀態列的高度,第二個為NavigationBar的高度,第三個為底部非安全區域的高度,iPhoneX比較特殊,並且WWDC上有明確說明對iPhoneX上view的顯示必須放在安全區域之內)。