適配 iOS 11 和 iPhone X

官方資源

WWDC 2017 Session 204: Updating Your App for iOS 11
WWDC視頻: Designing for iPhone X
為 iPhone X 更新您的 app
Apple: Layout Guides and Safe Area

博客資源

關于iOS11中estimatedRowHeight
  1. estimatedRowHeight 是一個預估高度,iOS 11 之前為 0,在 iOS11 下默認為44。
  2. 使用 estimatedRowHeight 屬性,可以將系統計算 contentSize 的高度的幾何計算成本從加載時推遲到滾動時再執行。
  3. 結論:當 cell 的實際高度大于預估高度的時候,會按照預估高度下的 cell 的數量來計算 contentSize ,當 cell 的實際高度小于預估高度的時候,會按照實際高度下的 cell 的數量來計算 contentSize。
  4. 使用 MJRefresh:(以下的問題在最新的版本已經修復了!)
    為什么使用 MJRefresh 在 iOS 11 下要設置 estimatedRowHeight=0?
    因為 MJRefresh 底部的上拉刷新是根據 contentSize 來計算的,當數據更新的時候,得出來的 contentSize 只是預估的。
  5. estimatedRowHeight 的值設置為 0,可以禁用此屬性:
override func viewDidLoad() { 
tableView.estimatedRowHeight = 0 
tableView.estimatedSectionHeaderHeight = 0 
tableView.estimatedSectionFooterHeight = 0
}
天天品嘗iOS7甜點 :: Day 20 :: View controller content and navigation bars
// iOS 11 中,`automaticallyAdjustsScrollViewInset` 屬性被棄用了。
@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets;

該屬性早年的作用:
默認為 YES,它允許容器視圖控制器知道它應該調整插入此視圖控制器視圖中的滾動視圖,以考慮狀態欄,搜索欄,導航欄,工具欄或選項卡欄消耗的屏幕區域。

WWDC 204: Updating Your App for iOS 11 筆記

  • UIBarItem

橫屏模式下,Tabbar 上的圖標會變小,長按可以彈出顯示大圖標。

UIBarItem.landscapeImagePhone
UIBarItem.LargeContentSizeImaage
  • 控制導航欄顯示大標題

主視圖控制器上顯示大圖標

// 設置導航欄顯示大標題
navigationBar.prefersLargeTitles = true

詳細視圖控制器只顯示小圖標

// 詳細視圖控制器中顯示正常標題
navigationItem.largeTitleDisplayMode = .never
  • 導航欄自動集成 UISearchController
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = YES;
  • UIToolbarUINavigationBar 擴展了新的自動布局支持

自定義的bar button items、自定義的 title 都可以通過 layout 來自定義尺寸。

Extensive new Auto Layout support

Keep your constraints inside your views

“We assume you know what you’re doing”

  • 避免零尺寸的自定義視圖

UINavigationBar and UIToolbar provide position

You must provide size

  • Constraints for width and height
  • Implement intrinsicContentSize
  • Connect your subviews via constraints
  • Margins and Insets

margins 指的是【控件顯示內容的邊緣】和【控件本身邊緣】之間的距離。

image
image

?

image

?
?
image

// systemMinimumLayoutMargins
viewRespectsSystemMinimumLayoutMargins = true // default
// directionalLayoutMargins
 
// edgesForExtendedLayout.top
// topLayoutGuide ?
// edgesForExtendedLayout.bottom
// bottomLayoutGuide ?
  • Safe Area

Describes area of view not occluded by ancestors ——用于描述不被遮擋的視圖區域

Available as insets or layout guide —— 可以用作插入量或者布局指引

Incorporates overscan compensation insets ——

image
image
  • Extending the Safe Area Insets——擴展安全區域插入量
// 通過 addtionalSafeAreaInsets 來改變 safeAreaInsets 的值
UIViewController.additionalSafeAreaInsets

// 獲取改變的回調:
UIView.safeAreaInsetsDidChange()
UIViewController.viewSafeAreaInsetsDidChange()

適配代碼

// 設置 view.top = self.view.top
CGFloat top;
if (@available(iOS 11.0, *)) {
    top = self.view.layoutMarginsGuide.layoutFrame.origin.y;
} else {
    top = 64;
}

// Masonry 框架
// 設置 View.top = self.view.top + 60
if (@available(iOS 11.0, *)) {
    make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).with.offset(0);
} else {
    make.top.equalTo(self.view.mas_top).with.offset(64);
}
  • Scroll View

adjustedContentInset

typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right;  // 指定每個邊的插入量(正數)。 值可能是負的(超出邊緣)。
} UIEdgeInsets;
  • Table View——在iOS 11中默認啟用Self-Sizing

UITableViewAutomaticDimension

// 關閉 estimateRowHeight 屬性
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

tableView.separatorInsetReference = .fromCellEdges // default
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容