iOS11适配&全局设置UIScrollViewContentI

2020-06-18  本文已影响0人  琦玉老师很强

相信对于iOS11的适配大家都应该不陌生,都是老生常谈的一些问题,其中在iOS11适配中关于UIScrollView的contentInsetAdjustmentBehavior属性, 这个属性的内部结构:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
    UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable
    UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
    UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
    UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));

1.UIScrollViewContentInsetAdjustmentAutomatic (类似于UIScrollViewContentInsetAdjustmentScrollableAxes,scrollView会自动计算和适应顶部和底部的内边距,并且在scrollView不可滚动时,也会设置内边距)
2.UIScrollViewContentInsetAdjustmentScrollableAxes (自动计算内边距)
3.UIScrollViewContentInsetAdjustmentNever (不计算内边距)
4.UIScrollViewContentInsetAdjustmentAlways (根据safeAreaInsets计算内边距)


处理方式:为了适配iOS11,我们需要把这个属性禁用调,使用UIScrollViewContentInsetAdjustmentNever不计算内边距,不然系统会帮我们自动计算内边距,这样在滚动之后无法定位,会出现额外的内边距偏差。
简化处理:之前项目内有大量的UIScrollView,UITableView和UICollectionVIew中单独处理iOS11的适配,导致大量的重复代码,其实没必要每次初始化的时候去单独处理这个属性,这几个控件都是遵循appearance,我们可以进行全局统一设置来简化代码。

- (void)adapterIOS11{
    // 适配iOS11以上UITableview 、UICollectionView、UIScrollview 列表/页面偏移
    if (@available(iOS 11.0, *)){
        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

        [[UITableView appearance] setEstimatedRowHeight:0];
        [[UITableView appearance] setEstimatedSectionFooterHeight:0];
        [[UITableView appearance] setEstimatedSectionHeaderHeight:0];
    }
}

然后在AppDelegate启动项目时,调用该方法,在didFinishLaunchingWithOptions中就能够完美的实现iOS11内边距偏移的适配。

上一篇下一篇

猜你喜欢

热点阅读