iOS11适配&全局设置

iOS11适配&全局设置,第1张

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));

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

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996518.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存