天天看點

App适配iOS 11

随着Xcode GM版本釋出,适配iOS 11也就提上了日程,總的來說整個适配過程(不包含适配iPhone X)不是很麻煩。

首先建議觀看今年WWDC的一個視訊 Updating Your App for iOS 11,視訊講解了iOS 11一些API的變化,對了解适配過程有幫助。

navigation bar

1、導航欄新增了一種大标題樣式,預設設定是不開啟,是以不需要修改。

2、titleView支援autolayout,這要求titleView必須是能夠自撐開的或實作了- intrinsicContentSize,簡書的搜尋就變成下面這樣了

App适配iOS 11

搜尋

解決辦法比較簡單,這個搜尋框對應的view實作- intrinsicContentSize方法

1 2 3

- (CGSize)intrinsicContentSize {

return

UILayoutFittingExpandedSize;

}

安全區域适配

iOS 11中ViewController的automaticallyAdjustsScrollViewInsets屬性被廢棄了,導緻了這兩個頁面出現了問題

App适配iOS 11
App适配iOS 11

這兩個頁面都隐藏了系統導航欄,自定義導航欄。

1 2 3

self.automaticallyAdjustsScrollViewInsets = NO;

self.extendedLayoutIncludesOpaqueBars = YES;

self.edgesForExtendedLayout = UIRectEdgeTop;

automaticallyAdjustsScrollViewInsets屬性被廢棄了,頂部就多了一定的inset,關于安全區域适配,簡書上的這篇文章iOS 11 安全區域适配總結介紹得非常詳細,請參考這篇文章。

我們采用了比較簡單的方法

1 2 3 4 5

if

(@available(iOS 

11.0

, *)) {

self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

else

{

self.automaticallyAdjustsScrollViewInsets = NO;

}

導航欄傳回按鈕

App适配iOS 11

之前的代碼通過下面的方式自定義傳回按鈕

1 2 3 4 5 6 7

UIImage *backButtonImage = [[UIImage imageNamed:@

"icon_tabbar_back"

]

resizableImageWithCapInsets:UIEdgeInsetsMake(

18

)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage

forState:UIControlStateNormal

barMetrics:UIBarMetricsDefault];

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(

, -

60

)

forBarMetrics:UIBarMetricsDefault];

iOS 11 中setBackButtonTitlePositionAdjustment:UIOffsetMake沒法把按鈕移出navigation bar。

解決方法是設定navigationController的backIndicatorImage和backIndicatorTransitionMaskImage

1 2 3

UIImage *backButtonImage = [[UIImage imageNamed:@

"icon_tabbar_back"

] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.navigationBar.backIndicatorImage = backButtonImage;

self.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;

tableview問題

App适配iOS 11

右邊為正确樣式

iOS 11中如果不實作-tableView: viewForFooterInSection: 和 -tableView: viewForHeaderInSection:,那麼-tableView: heightForHeaderInSection:和- tableView: heightForFooterInSection:不會被調用。

這是因為estimatedRowHeight estimatedSectionHeaderHeight estimatedSectionFooterHeight三個高度估算屬性由預設的0變成了UITableViewAutomaticDimension,導緻高度計算不對,解決方法是實作對應方法或吧這三個屬性設為0。

下面這個清單顯示不全也是estimatedRowHeight引起,取contentSize出錯。

App适配iOS 11

第三方依賴庫問題

1、ReactiveCocoa Unknown warning group ‘-Wreceiver-is-weak’,ignored警告

App适配iOS 11

ReactiveCocoa

簡書項目開啟Treat warning as error,所有警告都會被當成錯誤,是以必須解決掉。

RACObserve宏定義如下:

1 2 3 4 5 6 7 8

#define RACObserve(TARGET, KEYPATH) \

({ \

_Pragma(

"clang diagnostic push"

) \

_Pragma(

"clang diagnostic ignored \"-Wreceiver-is-weak\""

) \

__weak id target_ = (TARGET); \

[target_ rac_valuesForKeyPath:@keypath(TARGET, KEYPATH) observer:self]; \

_Pragma(

"clang diagnostic pop"

) \

})

在之前的Xcode中如果消息接受者是一個weak對象,clang編譯器會報receiver-is-weak警告,是以加了這段push&pop,最新的clang已經把這個警告給移除,是以沒必要加push&pop了。

ReactiveCocoa已經不再維護OC版本,大多數OC開發者用的都是2.5這個版本,隻能自己fork一份了,誰知github上的v2.5代碼不包含對應的.podspec檔案,隻好到CocoaPods/Specs上将對應的json檔案翻譯成.podspec檔案,如果你也有這個需要,可以修改Podfile如

1

pod 

'ReactiveCocoa'

, :git => 

'https://github.com/zhao0/ReactiveCocoa.git'

, :tag => 

'2.5.2'

2、MGSwipeTableCell 崩潰

App适配iOS 11

左滑cell

MGSwipeTableCell用于實作左滑菜單,在iOS 11上出現了崩潰,github上新版修複了,更新即可

轉載自: http://www.cocoachina.com/ios/20170915/20580.html