1. 程式人生 > >iOS 11 適配以及Xcode 9小Tips

iOS 11 適配以及Xcode 9小Tips

定義 dst iscroll ets forms shu -c contents methods

網上適配iOS 11的文章很多,但還是有些坑不踩不知道,這裏總結一下自己在項目中適配iOS 11的遇到的問題。

UIScrollView以及子類frame整體下移問題

之前的項目使用UIViewController的automaticallyAdjustsScrollViewInsets屬性,在iOS 11中被棄用,取而代之的是UIScrollView的 contentInsetAdjustmentBehavior。禁用在iOS11無法生效,導致整個頁面布局錯誤。

技術分享

iOS11api

解決辦法可以針對iOS 11用新API:

if (@available(iOS 11,*)) {
if ([tableview respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
}

若還想再VC層面進行禁用可以參考如下宏

#define  adjustsScrollViewInsets(scrollView)do {_Pragma("clang diagnostic push")_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"")if ([scrollView respondsToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {NSMethodSignature *signature = [UIScrollView instanceMethodSignatureForSelector:@selector(setContentInsetAdjustmentBehavior:)];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];NSInteger argument = 2;invocation.target = scrollView;invocation.selector = @selector(setContentInsetAdjustmentBehavior:);[invocation setArgument:&argument atIndex:2];[invocation retainArguments];[invocation invoke];}_Pragma("clang diagnostic pop")} while (0)

若項目中未使用automaticallyAdjustsScrollViewInsets屬性,則無需適配,繼續由系統控制就好。關於安全區域和邊距的詳細計算可參考安全區域適配總結。

UITableView的SetContentSize不準問題

iOS 11中默認開啟高度估算,在未實現viewForHeaderInSection時heightForHeaderInSection無法生效,關閉估算可以解決此bug。

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

[[UIScreen mainScreen] bounds].size不對

在運行iPhone X時,發現以前定義的屏幕size宏不對。原因在於此size取得是App啟動時加載的啟動圖Size,只需在啟動圖在加入iPhone X尺寸的啟動圖即可,分辨率為1125*2436。

導航欄item錯亂

項目中有的控制器需要自定義導航欄左右的barItem,當左右的barItem擠滿整個bar時,push然後pop回到本控制器時,baritem的frame直接變形。如下

技術分享

item

萬惡的根源是UINavigationBar中又加入了新的圖層UIButtonBarStackView,沒有自定義UINavigation真是個錯誤,迫不得已寫了新View蓋在Bar上面,手動控制起隱藏與顯示。這只是個臨時折中辦法,若有好的方法,記得分享給我~若只是位置偏移,可參考導航欄按鈕位置問題。

新iPhone的名稱

@"iPhone10,1" : @"iPhone 8",

@"iPhone10,4" : @"iPhone 8",

@"iPhone10,2" : @"iPhone 8 Plus",

@"iPhone10,5" : @"iPhone 8 Plus",

@"iPhone10,3" : @"iPhone X",

@"iPhone10,6" : @"iPhone X",

Xcode 9小Tips

1、導入圖片若失敗,手動在Build Phases --->Copy Binary With Libraies中重新添加一遍即可。

2、Xcode 9 打包時,記得在icon的添加1024*1024的圖標。

3、雞肋的無線調試功能(iPhone的電池...)可在Window -->Devices and Simulators中勾選那兩個選項。前提是此設備已run過並處於同一局域網下。

4、在Asset中,可以創建顏色了。右鍵選擇New image set,填充RGBA值或十六進制值即可。使用中直接使用新的colorwithname,參數填入創建時的名字即可。不過記得區分系統版本。

5、command鍵復原。可在Preferences --> Navigation -->Commadn-click 中選擇Jumps to Defintion即可。

6、xcode 9 還允許多開模擬器,不過公司的電腦。。。哎,不多說。

商業轉載請聯系作者獲得授權,非商業轉載請註明出處。

iOS 11 適配以及Xcode 9小Tips