scrollView的自动偏移问题
[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 是状态栏的高度。如果你想抵消这种偏移,添加
additionalSafeAreaInsets
参数。if (@available(iOS 11.0, *)) { self.table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; }