iOS学习专题IOS11适配iOS 11

Xcode9 & iOS11 适配

2017-08-08  本文已影响1024人  形而下的坏死

新增的 cmd + 左键 弹出菜单个人感觉很多余,想要改回原来直接跳转的同学可以在 Preferences - Navigation - Command-click on Code 改成 Jumps to Definition

Xcode9 warning :'xxx' is partial: introduced in iOS 10.0

Xcode9里面OC代码也支持swift的@available了,不再需要去写一串[[[UIDevice currentDevice] systemVersion] floatValue],在函数里面可以这么写

if (@available(iOS 10.0, *)) {
        
}
else {
   
}

然而现在并不能用,Xcode9打的包不能提交给Apple审核
另外用Xcode9打开原有代码时,下面的代码提示了'NCWidgetDisplayMode' is partial: introduced in iOS 10.0

- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
    if (activeDisplayMode == NCWidgetDisplayModeCompact) {
        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 110);
    } else {
        self.preferredContentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 300);
    }
}

在函数声明的时候不能使用@available,在SO查了一下,最后面加上NS_AVAILABLE_IOS(10_0)就可以消除这个warning

UITableView in iOS11

@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0); // default is UITableViewAutomaticDimension, set to 0 to disable

这三个属性是iOS7加入的,但是iOS11修改了它们的默认值,全部变成默认开启的状态。
这个修改产生了下面的问题

  1. UITableViewStyleGrouped样式的UITableView的sectionHeader和sectionFooter有一个默认的高度,通常不需要显示header或者footer的时候,会这么写
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

但是在iOS11里面你会发现段头段尾又回来辣!改了各种新增的属性比如safeArea之类的一点用都没有,最后发现必须要把estimatedSectionHeaderHeight置0才变回去

  1. safeArea引起的contentInset误差:一个全屏Frame的scrollView,iOS11会贴心地帮你在头部添加20px的contentInset,保证你的内容不覆盖住状态栏。
    但是之前写的时候想必已经考虑到这20px了,于是你的ScrollView突然多了20px的偏移,影响大小要看具体情况,当然最好是直接关掉这个特性
_list.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

iOS 11 下会自动给 frame 触及到safeArea 的scrollView 添加一个单独的 safeAreaInsets,inset 上部默认 44px(iPhone X为例),有 navigationBar 的情况下是 88px(注:如果设置 navigationBar.hidden = YES,上部会变成 44px),底部则是34 px,多数情况下直接把 scrollView 铺满屏幕即可,或者关闭 scrollView 的 contentInsetAdjustmentBehavior,单独处理。
但是有时候有需要配合底部工具栏(UIToolBar 会自动适配),那么需要对不同机型适配 safeArea ,iPhone X (44, 0, 34, 0),其余机型(20, 0, 0, 0),这个 UIEdgeInset 可以用下面的代码获取

//  需要 Xcode 9
- (UIEdgeInsets)safeAreaInset {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].keyWindow.safeAreaInsets;
    }
    else {
        return UIEdgeInsetsZero;
    }
}
  1. 调试发现tableView在某些情况下会用默认的cell高度(44px)去修正contentInset,例如top + bottom + contentSize计算出来的列表高度小于自己frame的时候。
    修正contentInset也会造成contentOffset的变动,我的页面有很多其他的视图和contentOffset有关,直接导致整个页面都乱了。
    这里和1相同,需要设置_list.estimatedRowHeight = 0

综上,如果没有用自动计算高度的习惯,推荐直接 hook 掉这三个属性

@implementation UITableView (DisableEstimateHeight)

+ (void)load
{
    Method newMethod = class_getInstanceMethod([UITableView class], @selector(hook_initWithFrame:style:));
    Method originMethod = class_getInstanceMethod([UITableView class], @selector(initWithFrame:style:));
    
    method_exchangeImplementations(newMethod, originMethod);
}

- (instancetype)hook_initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    UITableView *tableView = [self hook_initWithFrame:frame style:style];
    
    tableView.estimatedRowHeight = 0;
    tableView.estimatedSectionFooterHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    
    return tableView;
}

@end
上一篇 下一篇

猜你喜欢

热点阅读