iOS11适配&全局设置

博客介绍了在iOS11中,为适配UIScrollView的contentInsetAdjustmentBehavior属性,应将其设置为UIScrollViewContentInsetAdjustmentNever以避免自动计算内边距导致的滚动定位问题。此外,提出通过appearance统一设置UITableView、UICollectionView的适配策略,以减少重复代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值