iOS 11 UI 帶來粗體,動態(tài)的全新風(fēng)格踏揣。新風(fēng)格包括锻全,集成搜索功能的大標題導(dǎo)航欄奸攻,橫屏 Tab Bar,以及更強大的滑動手勢等等虱痕。
本文主要是 WWDC 2017 - Session 204 - Updating Your App for iOS 11一節(jié)的介紹睹耐。
Bar
UIBarItem
UITabBarItem 新增 largeContentSizeImage
屬性,在 Accessibility > Larger Text 中打開部翘,并調(diào)大字體硝训,長按 Item 會有 HUD 的放大效果。
UINavigationItem
UINavigationBar 新增 prefersLargeTitles
屬性新思,是否顯示大標題風(fēng)格窖梁。
大標題風(fēng)格 NavigationBar 高度為 116 px,iPhone X 另做討論夹囚。
UINavigationItem 新增 largeTitleDisplayMode
屬性纵刘,決定當(dāng)前 UIViewController 顯示大標題的方式。
-
Automatic
: 保持跟前一個控制器一致荸哟; -
Awayes
:始終顯示假哎; -
Never
:不顯示。
UIViewController 新增 searchController
集成 Search bar 方便快捷鞍历。
navigationItem.searchController = UISearchController(searchResultsController: nil)
navigationItem.hidesSearchBarWhenScrolling = false
Layout Guide
UIView 新增 directionalLayoutMargins
屬性舵抹,與layoutMargins
同步,不同的是在文字子控件縮進時,leading
代表文字起始位置的縮進劣砍,trailing
代表文字換行位置的縮進惧蛹。
UIViewController 新增 systemMinimumLayoutMargins
屬性,在 iOS 11 之前刑枝,Root View 的 layoutMarginsGuide
邊距默認是 16 或 20 px香嗓,是無法修改的。iOS 11 之后装畅, 可通過的directionalLayoutMargins
或者 LayoutMargins
修改靠娱, 如果新值比 systemMinimum 還要小,則會使用系統(tǒng)最小值洁灵,可以通過 viewRespectsSystemMinimumLayoutMargins
屬性饱岸,關(guān)閉最小值的限制掺出。
UIViewController topLayoutGuide
和 bottomLayoutGuide
被廢棄,取而代之的是 UIView 的 safeAreaLayoutGuide
苫费,可通過 additionalSafeAreaInsets
修改 safeArea汤锨。
UIViewController 的 automaticallyAdjustsScrollViewInsets
失效,當(dāng)我們自己需要設(shè)置 contentInset.top
時百框,會和系統(tǒng)自動設(shè)置的 64 px 造成沖突闲礼。取而代之的是 UIScrollView 的 contentInsetAdjustmentBehavior
屬性,同時可以自定義的設(shè)置 contentInset
铐维,并通過 adjustedContentInset
來獲取組合的 Inset柬泽。
UIScrollView 新增 frameLayoutGuide
和 contentLayoutGuide
為內(nèi)容的布局帶來更大的便捷。
UITableView
UITableView 默認會開啟 self-sizing 功能嫁蛇,F(xiàn)ooter锨并, Header,Cell 默認估算高度等于 UITableViewAutomaticDimension 睬棚。如果想關(guān)閉 iOS 11 的 self-sizing, 需要設(shè)置它們的高度都為 0第煮。
tableView.estimatedRowHeight = 0
tableView.estimatedSectionFooterHeight = 0
tableView.estimatedSectionHeaderHeight = 0
UITableview 的 Footer 和 Header 必須繼承自 UITableViewHeaderFooterView 以保證它們始終在 safeArea 內(nèi)。
UITableView 新增 separatorInsetReference
屬性抑党,separatorInset
默認 left 是從 cell 的左邊緣開始的包警,通過設(shè)置fromAutomaticInsets
可在系統(tǒng)默認的 16px 下進行偏移。
UITableView 新增兩種手勢控制代理方法底靠,使用如下:
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "Favorite") { (action, v, completion) in
// 修改數(shù)據(jù)源
completion(true)
}
action.backgroundColor = UIColor.blue
let configuration = UISwipeActionsConfiguration(actions: [action])
return configuration
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .destructive, title: "Delete") { (action, v, completion) in
// 修改數(shù)據(jù)源
completion(true)
}
let configuration = UISwipeActionsConfiguration(actions: [action])
return configuration
}
比較奇怪的是害晦,僅實現(xiàn) leadingSwipeActionsConfigurationForRowAt
方法,左滑的 Delete 按鈕也會出現(xiàn)暑中。