iOS11问题汇总

2,280 阅读4分钟
原文链接: www.jianshu.com

iOS11最终还是来了, 这次改版屏幕尺寸, 控件特性都有一些比较大的改变. 网上看的问题零零散散, 👇收集了一下开发中出现的问题, 整理一下, 方便之后使用.


目录概览
目录概览

1. 导航栏

导航栏高度一直是固定的64P, 到了iOS11这个规则被打破了, 除了iPhoneX全面屏, 刘海等适配问题, 还增加了大标题的属性, titleView支持autolayout;

大标题示例
大标题示例
//根据prefersLargeTitles设置, 默认为NO;
 self.navigationController.navigationBar.prefersLargeTitles = YES;

下方附一个导航栏的对比图:


一般机型导航栏尺寸图(除了iPhoneX)
一般机型导航栏尺寸图(除了iPhoneX)

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的新特性;

  • 设置delaysContentTouchesNO, 不会立即触发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: 添加内容到相册(写),会出现用户授权;

这些问题是网友们趟过的坑,希望简友们如果遇到过其他问题, 能积极补充, 共同进步;

题外话

一个人的国庆, 浪不起来, 写篇博客压压惊😓

参考文章:
关于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 适配指南