適配iOS 11和iPhone X

一、iOS 11新增API

關(guān)于iOS 11新增功能更詳細(xì)的介紹可以觀看WWDC2017
傳送門

1.large navigation titles——導(dǎo)航欄大標(biāo)題

導(dǎo)航欄標(biāo)題文字可以變的更大,因?yàn)閷?dǎo)航欄中加入了一個(gè)新的屬性prefersLargeTitles,默認(rèn)值是NO,不顯示導(dǎo)航條大標(biāo)題。

 /* When set to YES, the navigation bar will use a larger out-of-line
  * title view when requested by the current navigation item.
  * To specify when the large out-of-line title view appears, 
  * see UINavigationItem.largeTitleDisplayMode. Defaults to NO.*/

navigationBar.prefersLargeTitles = YES;
未設(shè)置.png

已設(shè)置.png

其次還有個(gè)屬性largeTitleDisplayMode,可以控制大標(biāo)題是否顯示,其值有Automatic(默認(rèn)值)和NeverAutomatic根據(jù)prefersLargeTitles的值自動(dòng)識(shí)別是否顯示,而Never不管prefersLargeTitles設(shè)置成什么值,都不顯示大標(biāo)題。

/* When UINavigationBar.prefersLargeTitles=YES, this property 
 * controls when the larger out-of-line title is displayed. 
 * If prefersLargeTitles=NO, this property has no effect. 
 * The default value is Automatic.*/

navigationItem.largeTitleDisplayMode = Automatic;
2.new search experience——導(dǎo)航條嵌入搜索欄

在navigationItem中結(jié)合了searchController,可以直接在導(dǎo)航欄內(nèi)側(cè)下添加一個(gè)搜索欄。

/* A view controller that will be shown inside of a navigation controller 
 * can assign a UISearchController to this property to 
* display the search controller’s search bar 
* in its containing navigation controller’s navigation bar.*/

UISearchController *search = [[UISearchController alloc] initWithSearchResultsController:self];
search.searchBar.placeholder = @"搜索";
self.navigationItem.searchController = search;
添加搜索欄.png

添加搜索欄后還可以設(shè)置該搜索欄是否根據(jù)滑動(dòng)隱藏

/* If this property is true (the default), 
 * the searchController’s search bar will hide as the user scrolls 
 * in the top view controller’s scroll view. If false, the search bar 
 * will remain visible and pinned underneath the navigation bar.*/

self.navigationItem.hidesSearchBarWhenScrolling = YES;
//其默認(rèn)值是YES,設(shè)置YES則搜索欄上滑自動(dòng)隱藏、下滑自動(dòng)顯示,這都是系統(tǒng)配置好的。
3.layout margins——布局空間

這三種布局供各位讀者了解,實(shí)際項(xiàng)目中應(yīng)用不怎么大,所以不做詳細(xì)說明了,稍微有用的就是第三個(gè)。

/* directionalLayoutMargins.leading is used on the left 
 * when the user interface direction is LTR and on the right for RTL.
 * Vice versa for directionalLayoutMargins.trailing. */

UIView.directionalLayoutMargins
/* Minimum layoutMargins for the view determined 
 * by the view controller from context and hardware information.
 * The view controller's view will respect these minimums unless       
 * viewRespectsSystemMinimumLayoutMargins
 * (which defaults to YES) is set to NO.
 */

UIViewController.systemMinimumLayoutMargins

此屬性包含系統(tǒng)為視圖控制器的根視圖所期望的最小布局邊距。不要重寫此屬性。停止考慮系統(tǒng)的最小版圖邊緣的根視圖,設(shè)置viewRespectsSystemMinimumLayoutMargins屬性為false。這個(gè)屬性不影響根視圖的子視圖相關(guān)聯(lián)的邊緣。
如果你的視圖控制器的根視圖的directionalLayoutMargins屬性指定自定義值,根視圖的實(shí)際利潤率設(shè)置為自定義值或最小值定義該屬性,兩者的值都大。例如,如果一個(gè)系統(tǒng)的最小邊距值為20個(gè)點(diǎn),并且在視圖上為相同的邊距指定一個(gè)值10,則視圖使用邊距的值20。

/* Default YES. 
 * The return value of the view's layoutMargins and directionalLayoutMargins properties 
 * will have values no smaller than the 
 * systemMinimumLayoutMargins. 
 * Set to NO for full customizability of the view's layoutMargins.
 */

UIViewController.viewRespectsSystemMinimumLayoutMargins
//如果在控制器中,UIViewController就用self代替

當(dāng)此屬性的值為true,根視圖的布局空間的保證是不小于在systemMinimumLayoutMargins屬性值。此屬性的默認(rèn)值為真。
更改此屬性為false能夠影響視圖只能獲取directionalLayoutMargins屬性的內(nèi)邊距。將該屬性中的邊距設(shè)置為0可以完全消除視圖的邊距。

4.safe area——安全區(qū)
/* Custom container UIViewController subclasses can 
 * use this property to add to the overlay
 * that UIViewController calculates for the safeAreaInsets for  
 * contained view controllers.
 */

UIViewController.additionalSafeAreaInset

使用此屬性來調(diào)整該視圖控制器的視圖指定數(shù)量的安全區(qū)的插圖。安全區(qū)域定義了視圖控制器的可見區(qū)域的部分,可以保證不受系統(tǒng)狀態(tài)欄的影響。您可以使用此屬性擴(kuò)展安全區(qū)域,以便在接口中包含自定義內(nèi)容。例如,一個(gè)繪圖應(yīng)用程序可以使用這個(gè)屬性來避免內(nèi)容顯示在工具欄。

UIViewController.viewSafeAreaInsetsDidChange()
UIViewController.viewLayoutMarginsDidChange()

使用此方法更新您的接口以適應(yīng)新的安全區(qū)域。UIKit更新安全區(qū)響應(yīng)大小的變化對(duì)系統(tǒng)的酒吧或當(dāng)你修改你的視圖控制器的附加安全區(qū)的插圖。UIKit也立即調(diào)用此方法之前,你的觀點(diǎn)出現(xiàn)在屏幕上。

UIView.safeAreaInsets
UIView.safeAreaInsetsDidChange()
5.scrollView
/* When contentInsetAdjustmentBehavior allows, 
 *UIScrollView may incorporate its safeAreaInsets into the adjustedContentInset.
 */

UIScrollView.adjustedContentInset

iOS 11中的adjustedContentInset代替了其他版本中的contentInset,新增的contentInsetAdjustmentBehavior屬性需要用來配置adjustedContentInset。

/* frameLayoutGuide anchors 
 * (e.g.,frameLayoutGuide.centerXAnchor) refer to the untransformed frame of the scroll view.
 */

UIScrollView.frameLayoutGuide

負(fù)責(zé)UIScrollView在屏幕中的大小和位置。

/* contentLayoutGuide anchors (e.g., contentLayoutGuide.centerXAnchor, etc.) refer to
 the untranslated content area of the scroll view.
 */

UIScrollView.contentLayoutGuide

負(fù)責(zé)設(shè)置UIScrollView的內(nèi)容位置大小或者讓內(nèi)容隨著滾動(dòng)而移動(dòng)。

/* Configure the behavior of adjustedContentInset.
 Default is UIScrollViewContentInsetAdjustmentAutomatic.
 */

UIScrollView.contentInsetAdjustmentBehavior

UIScrollViewContentInsetAdjustmentBehavior是一個(gè)枚舉類型,值有以下幾種:
automaticscrollableAxes一樣,scrollView會(huì)自動(dòng)計(jì)算和適應(yīng)頂部和底部的內(nèi)邊距并且在scrollView不可滾動(dòng)時(shí),也會(huì)設(shè)置內(nèi)邊距。
scrollableAxes自動(dòng)計(jì)算內(nèi)邊距。
never不計(jì)算內(nèi)邊距。
always根據(jù)safeAreaInsets計(jì)算內(nèi)邊。

6.tableView
// default is UITableViewAutomaticDimension, set to 0 to disable

UITableView.estimatedRowHeight
UITableView.estimatedSectionHeaderHeight
UITableView.estimatedSectionFooterHeight

如果代碼中沒有設(shè)置cell行高或者沒有重寫相應(yīng)的代理方法,那么系統(tǒng)會(huì)自動(dòng)給行、頁眉和頁腳設(shè)置一個(gè)估計(jì)值。
如果要適配各個(gè)iOS版本,最好在AppDelegate中添加:

if (@available(iOS 11.0, *)) {
        UITableView.appearance.estimatedRowHeight = 0;
        UITableView.appearance.estimatedSectionFooterHeight = 0;
        UITableView.appearance.estimatedSectionHeaderHeight = 0;
    }
//appearance設(shè)置所有的tableView的行、頁眉和頁腳估計(jì)值全部為0。
/* Changes how custom separatorInset values are interpreted. 
 * The default value is UITableViewSeparatorInsetFromCellEdges*/

UITableView.separatorInsetReference

改變默認(rèn)內(nèi)嵌位置

7.語法警告自動(dòng)糾錯(cuò)功能
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官方資源 WWDC 2017 Session 204: Updating Your App for iOS 11 ...
    獨(dú)木舟的木閱讀 762評(píng)論 0 4
  • iPhone 的 navagationbar 高度 為 88, [[UIApplication sharedApp...
    dingzhijie閱讀 432評(píng)論 0 1
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,162評(píng)論 1 92
  • iOS 11正式發(fā)布了,下面整理了一些該版本下的特點(diǎn)還有如何進(jìn)行兼容適配工作。 1. UIView變化 1.1. ...
    杰嗒嗒的阿杰閱讀 11,536評(píng)論 14 46
  • ——和繁華落盡深眸《合歡樹》 粉蕊迎朝靄,瑤枝映暮輝。 數(shù)滴癡戀淚,幾喚浪兒回。 苦誓合歡應(yīng),愁殺彩練偎。 夕顏何...
    不惑而歌閱讀 542評(píng)論 19 28

友情鏈接更多精彩內(nèi)容