1. 程式人生 > >ios 11 螢幕適配問題!

ios 11 螢幕適配問題!

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

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

1、導航欄新增了一種大標題樣式,預設設定是不開啟,所以不需要修改。
2、titleView支援autolayout,這要求titleView必須是能夠自撐開的或實現了- intrinsicContentSize,簡書的搜尋就變成下面這樣了

搜尋
搜尋

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

方法

- (CGSize)intrinsicContentSize {
    return UILayoutFittingExpandedSize;
}

安全區域適配

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

image.png
image.png image.png
image.png

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

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

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

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

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

導航欄返回按鈕

image.png
image.png

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

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的backIndicatorImagebackIndicatorTransitionMaskImage

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出錯。
image.png
image.png

第三方依賴庫問題

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

ReactiveCocoa
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
左滑cell

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