iOS11 适配问题(tableView和scrollView)及解决方案

tableView--问题
在iOS11上,tableview的 Headers, footers, and cells都默认开启Self-Sizing,所有estimated 高度默认值从iOS11之前的 0 改变为UITableViewAutomaticDimension。
在使用tableview的界面上,与11之前相比,cell下移;使用header的界面,默认
会有footer的高度,导致下一个cell的header展示不友好。
解决方案
iOS11 tableview会默认开启self-sizing,
开启Self-Sizing之后,tableView是使用estimateRowHeight属性的,这样就会造成contentSize和contentOffset值的变化。
通过设置:
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;
解决上述问题。

scrollView--问题
iOS9.3.5


image.png

iOS 11.1


image.png

对比contentsize,iOS11比iOS9 高度上增加。
iOS11 上出现了新的属性,adjustedContentInset
并且会默认设置。


image.png

可以通过contentInsetAdjustmentBehavior属性用来配置adjustedContentInset的行为

image.png

UIScrollViewContentInsetAdjustmentBehavior 结构体
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
UIScrollViewContentInsetAdjustmentAutomatic,
UIScrollViewContentInsetAdjustmentScrollableAxes,
UIScrollViewContentInsetAdjustmentNever,
UIScrollViewContentInsetAdjustmentAlways,
}

so,设置contentInsetAdjustmentBehavior 为never
scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
因为上述属性是ios11新属性,需要加上可用性判断

 if (@available(iOS 11.0,*)) {
    scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

@available(iOS 11.0,*) 是swift用法,Swift 2.0 中,引入了可用性的概念。对于函数,类,协议等,可以使用@available声明这些类型的生命周期依赖于特定的平台和操作系统版本。

如果我们一个个去寻找有问题的界面,逐个解决,未免也太不效率了。所以我们使用了aop思想,此处不做深入展开,需要了解的请搜索Aspects。我们通过监听UIView的addSubview:方法,在此之前,通过判断添加的view匹配UITableView和UIScrollView,添加上述的解决方案代码。代码如下:

    [UIView aspect_hookSelector:@selector(addSubview:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info){
        @try {
            if ([[info instance] isKindOfClass:[UITableView class]]) {
                UITableView *tb = (UITableView *)[info instance];
                tb.estimatedSectionHeaderHeight = 0;
                tb.estimatedSectionFooterHeight = 0;
            }
            if ([[info instance] isKindOfClass:[UIScrollView class]]) {
                UIScrollView *scrollview = (UIScrollView *)[info instance];
                if (@available(iOS 11.0,*)) {
                    scrollview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
                }
            }
        } @catch (NSException *exception) {
            NSLog(@"%@",exception.name);
        }
    } error:NULL];

有不正之处,请指出,感谢各位的建议和意见!

参考文章:
你可能需要为你的APP适配

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 苹果WWDC开发者大会上,终于发布了大家期待已久的iOS 11,有些新特性功能确实出人意料。不过大的方面苹果...
    Mr_Say_Yes阅读 3,352评论 6 15
  • UIScrollView and UITableView的新特性 ScrollView 如果有一些文本位于UI滚动...
    透支未来阅读 617评论 0 0
  • 场景一: 刚刚哄娃睡,陈先生睡我左边(小屁孩强烈要求爸爸陪睡),小屁孩睡我右边,有阵吹进来,陈先生下意识地去拖被子...
    初升的太阳曈曈阅读 697评论 0 1
  • 很多人有这样的感受:一上班,就连轴转,下了班,发现要做的事情一样没做。 那种挫败与沮丧,同时不停的问自己:白天我干...
    等姐阅读 138评论 0 2
  • 一杯敬自由 一杯敬远方 你有多久没出去看看了?
    简糖不苦阅读 258评论 0 1