iOS 滚动视图内容自动偏移总结
[iOS7 , iOS11):
viewcontroller参数automaticallyAdjustsScrollViewInsets决定其中scrollview偏移量参数contentInset:
默认情况下:automaticallyAdjustsScrollViewInsets = YES,contentInset = {64,0,0,0},tabbarcontroller情况下为{64,0,49,0},很容易理解假设设置scorllview的frame为屏幕大小,其可控高度为其高度减掉导航条的64和tabbar的49
导航条隐藏时,两种情况下contentInset分别是{20,0,0,0},{20,0,49,0},20是状态栏的高度
[iOS11,~):
viewcontroller新增所谓安全区域,带来一系列参数:SafeAreaInsets、additionalSafeAreaInsets、adjustedContentInset、contentInsetAdjustmentBehavior……;
原来的automaticallyAdjustsScrollViewInsets失效了,新的方案认为状态栏导航条、tabbar之间的区域为安全区域
scrollview的最终偏移量adjustedContentInset = safeAreaInset + contentInset,safeAreaInset的值与iOS 11之前的contentInset相同,iOS11中contentInset默认为{0,0,0,0}
导航条隐藏时,两种情况下safeAreaInset分别是{20,0,0,0},{20,0,49,0},20是状态栏的高度
contentInsetAdjustmentBehavior参数:
UIScrollViewContentInsetAdjustmentAutomatic:如果scrollview在一个controller上,并且这个Controller包含在一个navigation controller中,这种情况下会设置在top & bottom上 adjustedContentInset = safeAreaInset + contentInset不管是否滚动。其他情况下与UIScrollViewContentInsetAdjustmentScrollableAxes相同
UIScrollViewContentInsetAdjustmentScrollableAxes: 在可滚动方向上adjustedContentInset = safeAreaInset + contentInset,在不可滚动方向上adjustedContentInset = contentInset;依赖于scrollEnabled和alwaysBounceHorizontal / vertical = YES,scrollEnabled默认为yes,所以大多数情况下,计算方式还是adjustedContentInset = safeAreaInset + contentInset
UIScrollViewContentInsetAdjustmentNever: adjustedContentInset = contentInset
UIScrollViewContentInsetAdjustmentAlways: adjustedContentInset = safeAreaInset + contentInset
当contentInsetAdjustmentBehavior设置为UIScrollViewContentInsetAdjustmentNever的时候,adjustContentInset值不受SafeAreaInset值的影响。
如果你被苹果这个设计恶心到了
if (@available(iOS 11.0, *)) { self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } }
另外注意:edgesForExtendedLayout和extendedLayoutIncludesOpaqueBars
这两个参数导致的view偏移问题