簡書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,簡書的搜索就變成下面這樣了

搜索

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

Advertisements

- (CGSize)intrinsicContentSize { return UILayoutFittingExpandedSize;}

安全區域適配

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

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

self.automaticallyAdjustsScrollViewInsets = NO;self.extendedLayoutIncludesOpaqueBars = YES;self.edgesForExtendedLayout = UIRectEdgeTop;

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

Advertisements

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

if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;} else { self.automaticallyAdjustsScrollViewInsets = NO;}

導航欄返回按鈕

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

UIImage *backButtonImage = [[UIImage imageNamed:@"icon_tabbar_back"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 18, 0, 0)];[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

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

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

UIImage *backButtonImage = [[UIImage imageNamed:@"icon_tabbar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];self.navigationBar.backIndicatorImage = backButtonImage;self.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;

tableview問題

右邊為正確樣式

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

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

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

第三方依賴庫問題

1、ReactiveCocoa Unknown warning group 『-Wreceiver-is-weak』,ignored警告

ReactiveCocoa

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

RACObserve宏定義如下:

#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如

pod 'ReactiveCocoa', :git => 'https://github.com/zhao0/ReactiveCocoa.git', :tag => '2.5.2'

2、MGSwipeTableCell 崩潰

左滑cell

MGSwipeTableCell用於實現左滑菜單,在iOS 11上出現了崩潰,github上新版修復了,升級即可

最後,感謝我女朋友在我寫這篇文章的時候喂我吃水果。

著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。

Advertisements

你可能會喜歡