iOS11最終還是來了, 這次改版屏幕尺寸, 控件特性都有一些比較大的改變. 網上看的問題零零散散, ??收集了一下開發中出現的問題, 整理一下, 方便之后使用.
1. 導航欄
導航欄高度一直是固定的64P, 到了iOS11這個規則被打破了, 除了iPhoneX全面屏, 劉海等適配問題, 還增加了大標題的屬性,
titleView
支持autolayout
;
大標題示例//根據prefersLargeTitles設置, 默認為NO; self.navigationController.navigationBar.prefersLargeTitles = YES;
下方附一個導航欄的對比圖:
一般機型導航欄尺寸圖(除了iPhoneX)
iPhoneX導航欄尺寸圖
1.1 尺寸適配
sol 1: 宏定義高度
#define NAVIGATION_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + CGRectGetHeight(self.navigationController.navigationBar.frame))
#ifdef __IPHONE_11_0
if (@available(iOS 11.0, *)) {
self.navigationBar.frame = CGRectMake(0, STATUSBAR_HEIGHT,ScreenWidth, NAVIGATION_HEIGHT);
}
#endif
sol 2: 工具類獲取高度
@implementation TestUtil
+ (CGFloat)navigationBarHeight {
if (IS_iPhoneX) {
return 88.0f;
}
return 64.0f;
}
@end
#define IS_iPhoneX ([UIScreen mainScreen].bounds.size.width == 375 && [UIScreen mainScreen].bounds.size.height == 812)
1.2 titleView擴展
//titleView自擴展尺寸, 需要自定義view實現該方法
- (CGSize)intrinsicContentSize {
return UILayoutFittingExpandedSize;
}
1.3 返回按鈕偏移
返回按鈕向下偏移;
//設置navigationController的backIndicatorImage和backIndicatorTransitionMaskImage
UIImage *backButtonImage = [[UIImage imageNamed:@"icon_tabbar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationBar.backIndicatorImage = backButtonImage;
self.navigationBar.backIndicatorTransitionMaskImage = backButtonImage;
2. 安全區域(內容部分)
關于安全區域, tableView廢棄了
automaticallyAdjustsScrollViewInsets
屬性;estimatedRowHeight
,estimatedSectionHeaderHeight
estimatedSectionFooterHeight
三個高度估算屬性由默認的0變成了UITableViewAutomaticDimension
;當然了, 這個是造成UI紊亂的原因, 值得一提的是tableView的新特性;
- 設置
delaysContentTouches
為NO
, 不會立即觸發cell的響應事件;- 兩根手指快速的輕擊cell,可以同時選中兩個cell進入編輯狀態。如果兩個手指存在不同步問題,則會默認識別其中的一個手指表示單選cell;
- 新增了一個屬性
separatorInsetReference
可以自定義一個cell分割線的邊距;- cell或者表頭表尾默認采用自適應高度的方案(造成UI紊亂的原因);
- 增加了
numberOfLines
屬性來實現類似于UILabel一樣的高度自適應變化;
2.1 頂部偏移
automaticallyAdjustsScrollViewInsets
屬性被廢棄,頂部就多了一些偏移;
**sol 1: **
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
**sol 2: **
//iOS11 解決SafeArea的問題,同時能解決pop時上級頁面scrollView抖動的問題
if (@available(iOS 11, *)) {
[UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
**sol 3: **
#define adjustsScrollViewInsets_NO(scrollView,vc)\do { \_Pragma("clang diagnostic push") \_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\[scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\} else {\vc.automaticallyAdjustsScrollViewInsets = NO;\}\_Pragma("clang diagnostic pop") \} while (0)
網友還出現了webView會向下移動部分距離的問題;
if (@available(iOS 11.0, *)) {
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
}
2.2 cell間隔變大
**sol 1: **
self.table.estimatedRowHeight = 0;
self.table.estimatedSectionHeaderHeight = 0;
self.table.estimatedSectionFooterHeight = 0;
**sol 2: **
//解決iOS11,僅實現heightForHeaderInSection,沒有實現viewForHeaderInSection方法時,section間距大的問題
[UITableView appearance].estimatedRowHeight = 0;
[UITableView appearance].estimatedSectionHeaderHeight = 0;
[UITableView appearance].estimatedSectionFooterHeight = 0;
3. 底部區域(tabBar)
底部區域主要是iPhoneX與其他機型不一樣, 一般機型高度為49, iPhoneX為83;
適配問題
#define kTabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
4. 其他問題
在IOS11,原有的
NSLocationAlwaysUsageDeion
被降級為NSLocationWhenInUseUsageDeion
;
4.1 系統獲取權限框未彈出
需要在plist文件中配置NSLocationAlwaysAndWhenInUseUsageDeion
,系統框才會彈出;使用requestAlwaysAuthorization獲取權限 IOS11系統彈框會把幾種權限級別全部列出,供用戶選擇;
NSLocationUsageDescription
獲取地理位置,精準推送服務
NSLocationWhenInUseUsageDescription
獲取地理位置,精準推送服務
NSLocationAlwaysUsageDescription
App需要您的同意,才能始終訪問位置
NSLocationAlwaysAndWhenInUseUsageDeion
App需要您的同意,才能始終訪問位置
4.2 iPhone X上運行緩沖頁有黑色區域
sol 1: 添加iPhoneX的Launch圖1125x2436
使用LaunchScreen來當做緩沖頁或者修改Assets中的LaunchImage,添加iPhoneX的Launch圖1125*2436(豎屏);
sol 2: 修改Contents.json文件
{
"extent" : "full-screen",
"idiom" : "iphone",
"subtype" : "2436h",
"filename" : "1125_2436.png”,
"minimum-system-version" : "11.0",
"orientation" : "portrait",
"scale" : "3x"
}
sol 2: 使用 LaunchScreen.storyboard 設置啟動圖
使用 LaunchScreen.storyboard 文件將簡單視圖約束定位,實現各種尺寸的自適應。
4.3 第三方依賴庫問題
ReactiveCocoa Unknown warning group ‘-Wreceiver-is-weak’,ignored警告;
#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") \
})
4.4 xib編譯問題
一般是打開以前的工程, 編譯的時候xib報錯, 例如:
warning: Internationalization is not available when compiling for targets before iOS 6.0
解決辦法:
選擇編譯錯誤的xib文件,找到Builds for 改為iOS 7.0 and Later
4.5 相冊權限變更
iOS11以前:
NSPhotoLibraryUsageDescription
:訪問相冊和存儲照片到相冊(讀寫),會出現用戶授權;
iOS11之后:
NSPhotoLibraryUsageDescription
:無需添加。默認開啟訪問相冊權限(讀),無需用戶授權;
NSPhotoLibraryAddUsageDescription
: 添加內容到相冊(寫),會出現用戶授權;
4.6 跳轉App Store評論變更
iOS11以前:
- (void)goToAppStore
{
NSString *itunesurl = @"[http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=XXXXXXXX&pageNumber=0&sortOrdering=2&type=Purple](http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=XXXXXXXX&pageNumber=0&sortOrdering=2&type=Purple)+Software&mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:itunesurl]];
}
iOS11之后:
- (void)goToAppStore
{
NSString *itunesurl = @
"itms-[apps://itunes.apple.com/cn/app/idXXXXXX?mt=8&action=write-review](apps://itunes.apple.com/cn/app/idXXXXXX?mt=8&action=write-review)";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:itunesurl]];
}
注:XXX為你自己的APP ID;
這些問題是網友們趟過的坑,希望簡友們如果遇到過其他問題, 能積極補充, 共同進步;
題外話
一個人的國慶, 浪不起來, 寫篇博客壓壓驚??
參考文章:
關于iPhone X、iOS 11 、Xcode9,我們應該知道這些
iOS11就問你一句“驚不驚喜?意不意外?”5.8的苦笑。。。。。
iOS11 適配之導航欄、tableView、searchBar遇到的bug
簡書App適配iOS 11
10分鐘適配 iOS 11 & iPhone X
iOS11開發 新增功能大全
iPhone X + iOS 11 適配指南
iOS 11適配之跳轉App Store評論