最近项目升级到iOS 11,遇到一些问题,在此记录一下!
一. push 新界面,出现一个从左往右并且从下往上移动的问题
解决办法:
设置UIScrollView的 contentInsetAdjustmentBehavior的属性值为UIScrollViewContentInsetAdjustmentNever就可以。
二. 图片保存到相册崩溃问题
iOS11 图片保存到相册需要在在plist里面权限说明添加:
<key>NSPhotoLibraryAddUsageDescription</key>
<string>相册添加图片权限</string>
三. tableView 向下偏移20问题适配
// tableView 偏移20/64适配
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
四. tableView 如果是Gruop类型的话,section之间的间距变宽,执行返回高度的同时还需要执行return UIView的代理,才起作用。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] init];
}
五.tableView 刷新 或者 滚动 出现异常问题。
解决方法:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
因为在iOS 11中默认启用Self-Sizing,Headers, footers, and cells都默认开启Self-Sizing, 所有estimated高度默认值从iOS 11之前的0改变为UITableViewAutomaticDimension。
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable
如果目前项目中没有启用estimateRowHeight属性,在iOS11的环境下就要注意,因为开启Self-Sizing之后,tableView是使用estimateRowHeight属性的,这样就会造成contentSize 和 contentOffset值变化,如果是有动画观察这两个属性的变化进行的,就会造成动画异常,因为在估算行高机制下,contentSize的值是一点点地变化更新的,所有cell显示完成后才得到最终的contentSize值。因为不会缓存正确的行高,[tableView raloadData]的时候,会重新计算contentSize, 就有可能引起contentOffset的变化。iOS11不想使用Self-Sizing的话,可以通过以下方式关闭:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
六.__NSArrayI类型和__NSArrayM类型直接通过索引获取内容,底部调用函数更改为:
- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx API_AVAILABLE(macos(10.8), ios(6.0), watchos(2.0), tvos(9.0));
所以如果通过拦截器,拦截防止数组越界,应对这个函数进行拦截。具体可以看:FJAvoidCrashMechanism。
七.自定义导航栏 偏移问题
在Xcode 9.0,iOS 11下,自定义导航栏呈如下状态:

具体原因可以查看这篇文章: App界面适配iOS11(包括iPhoneX的奇葩尺寸
解决办法:
在自定义导航栏内部进行判断:
#import "DJMsgListNavigationBar.h"
@implementation DJMsgListNavigationBar
#pragma mark --- layout method
-(void)layoutSubviews{
[super layoutSubviews];
CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
if (rectStatus.size.height==44.f) {
}else{
if (@available(iOS 11.0, *)) {
for ( UIView*aView in self.subviews) {
if ([NSStringFromClass(aView.classForCoder) isEqualToString:@"_UINavigationBarContentView"]) {
aView.frame = CGRectMake( 0,20,aView.frame.size.width,44);
}
else if ([NSStringFromClass(aView.classForCoder) isEqualToString:@"_UIBarBackground"]) {
aView.frame = CGRectMake(0,0,aView.frame.size.width, 64);
}
}
}
}
}
