iOS 11 適配

來源:互聯網
上載者:User

標籤:包含   group   push   inter   mat   設定   oid   idt   頁面   

解決push VC , tabbar 跳動問題:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.viewControllers.count > 0) { viewController.hidesBottomBarWhenPushed = YES; } [super pushViewController:viewController animated:animated]; // 修改tabBra的frame CGRect frame = self.tabBarController.tabBar.frame; frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height; self.tabBarController.tabBar.frame = frame; }



導語:本文主要是對iOS 11下APP中tableView內容下移20pt或下移64pt的問題適配的一個總結。內容包括五個部分:問題的原因分析、adjustContentInset屬性的計算方式、什麼情況下的tableView會發生內容下移、有哪些解決方案、解決這個問題時遇到的另外一個小問題。

一、iOS 11下APP中tableView內容下移20pt或下移64pt的原因分析

問題如所示:


問題.png1. 原因分析

原因是iOS 11中Controller的automaticallyAdjustsScrollViewInsets屬性被廢棄了,所以當tableView超出安全區域時系統自動調整了SafeAreaInsets值,進而影響adjustedContentInset值,在iOS 11中決定tableView的內容與邊緣距離的是adjustedContentInset屬性,而不是contentInsetadjustedContentInset的計算方式見本文第二部分內容。因為系統對adjustedContentInset值進行了調整,所以導致tableView的內容到邊緣的距離發生了變化,導致tableView下移了20pt(statusbar高度)或64pt(navigationbar高度)。

如果你的APP中使用的是自訂的navigationbar,隱藏掉系統的navigationbar,並且tableView的frame為(0,0,SCREEN_WIDTH, SCREEN_HEIGHT)開始,那麼系統會自動調整SafeAreaInsets值為(20,0,0,0),如果使用了系統的navigationbar,那麼SafeAreaInsets值為(64,0,0,0),如果也使用了系統的tabbar,那麼SafeAreaInsets值為(64,0,49,0)。關於什麼情況下會發生內容下移的問題,本文第三部分有介紹。

2. 安全區域的概念

系統自動調整tableView內容位移量,是根據安全區域來調整的。安全區域是iOS 11新提出的,如所示:



SafeArea of an interface.png

安全區域協助我們將view放置在整個螢幕的可視的部分。即使把navigationbar設定為透明的,系統也認為安全區域是從navigationbar的bottom開始的。
安全區域定義了view中可視地區的部分,保證不被系統的狀態列、或父視圖提供的view如導覽列覆蓋。可以使用additionalSafeAreaInsets去擴充安全區域去包括自訂的content在你的介面。每個view都可以改變安全區域嵌入的大小,Controller也可以。

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

二、 adjustContentInset屬性的計算方式

首先看scrollView在iOS11新增的兩個屬性:adjustContentInsetcontentInsetAdjustmentBehavior

/* Configure the behavior of adjustedContentInset.Default is UIScrollViewContentInsetAdjustmentAutomatic.*/@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior

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

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

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

  3. UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset

  4. UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset

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

三、什麼情況下的tableView會發生上述問題

如果設定了automaticallyAdjustsScrollViewInsets = YES,那麼不會發生問題,一直都是由系統來調整內容的位移量。

接下來排查下自己的項目中哪些頁面會發生以上問題。

當tableView的frame超出安全區域範圍時,系統會自動調整內容的位置,SafeAreaInsets值會不為0,於是影響tableView的adjustContentInset值,於是影響tableView的內容展示,導致tableView的content下移了SafeAreaInsets的距離。SafeAreaInsets值為0時,是正常的情況。

需要瞭解每個頁面的結構,看tableView是否被系統的statusbar或navigationbar覆蓋,如果被覆蓋的話,則會發生下移。也可以通過tableview.safeAreaInsets的值來確認是因為安全區域的問題導致的內容下移。

如下程式碼片段,可以看出系統對tableView向下調整了20pt的距離,因為tableView超出了安全區域範圍,被statusbar覆蓋。

tableview.contentInset: {64, 0, 60, 0}tableview.safeAreaInsets: {20, 0, 0, 0}tableview.adjustedContentInset: {84, 0, 60, 0}
四、這個問題的解決方案有哪些?1. 重新設定tableView的contentInset值,來抵消掉SafeAreaInset值,因為內容下移位移量 = contentInset + SafeAreaInset;

如果之前自己設定了contentInset值為(64,0,0,0),現在系統又設定了SafeAreaInsets值為(64,0,0,0),那麼tableView內容下移了64pt,這種情況下,可以設定contentInset值為(0,0,0,0),也就是遵從系統的設定了。

2. 設定tableView的contentInsetAdjustmentBehavior屬性

如果不需要系統為你設定邊緣距離,可以做以下設定:

 //如果iOS的系統是11.0,會有這樣一個宏定義“#define __IPHONE_11_0  110000”;如果系統版本低於11.0則沒有這個宏定義#ifdef __IPHONE_11_0   if ([tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {    tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;}#endif

contentInsetAdjustmentBehavior屬性也是用來取代automaticallyAdjustsScrollViewInsets屬性的,推薦使用這種方式。

3. 通過設定iOS 11新增的屬性addtionalSafeAreaInset;

iOS 11之前,大家是通過將Controller的automaticallyAdjustsScrollViewInsets屬性設定為NO,來禁止系統對tableView調整contentInsets的。如果還是想從Controller層級解決問題,那麼可以通過設定Controller的additionalSafeAreaInsets屬性,如果SafeAreaInset值為(20,0,0,0),那麼設定additionalSafeAreaInsets屬性值為(-20,0,0,0),則SafeAreaInsets不會對adjustedContentInset值產生影響,tableView內容不會顯示異常。這裡需要注意的是addtionalSafeAreaInset是Controller的屬性,要知道SafeAreaInset的值是由哪個Controller引起的,可能是由自己的Controller調整的,可能是navigationController調整的。是由哪個Controller調整的,則設定哪個Controller的addtionalSafeAreaInset值來抵消掉SafeAreaInset值。

五、遇到的另外一個與安全區域無關的tableView內容下移的問題

我的作品頁面的tableView下移了約40pt,這裡是否跟安全區域有關呢?

查了下頁面結構,tableView的父視圖的frame在navigationbar的bottom之下,tableView在父視圖的安全區域內,列印出來tableView的SafeAreaInset值也是(0,0,0,0);所以不是安全區域導致的內容下移。

經過查看代碼,發現tableView的style:UITableViewStyleGrouped類型,預設tableView開頭和結尾是有間距的,不需要這個間距的話,可以通過實現heightForHeaderInSection方法(返回一個較小值:0.1)和viewForHeaderInSection(返回一個view)來去除頭部的留白,底部同理。

iOS 11上發生tableView頂部有留白,原因是代碼中只實現了heightForHeaderInSection方法,而沒有實現viewForHeaderInSection方法。那樣寫是不規範的,只實現高度,而沒有實現view,但代碼這樣寫在iOS 11之前是沒有問題的,iOS 11之後應該是由於開啟了估算行高機制引起了bug。添加上viewForHeaderInSection方法後,問題就解決了。或者添加以下代碼關閉估算行高,問題也得到解決。

self.tableView.estimatedRowHeight = 0;self.tableView.estimatedSectionHeaderHeight = 0;self.tableView.estimatedSectionFooterHeight = 0;

iOS 11 適配

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.