scrollview的几个关键属性
contentSize: 就是scrollView可以滚动的区域. 例如 frame = (0,0, 320, 480)
, contentSize = (320, 960)
, 代表scrollView可以上下滚动, 滚动区域为frame大小的两倍.
contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量, 比如上一个例子中, 将scrollView拉到最下面, 那么contentOffset = (0, 480)
, 也就是 y 偏移了480.
contentInset是scrollView中的contentView.frame.origin
和scrollView.frame.origin
之间的关系. 例如contentView.frame = (0, 30, 320, 480)
, 那么contentInset = (0, 30)
iOS11 新加入的属性 adjustedContentInset
: 表示scrollView的内容的自适应内边距, 它的取值和contentInsetAdjustmentBehavior
枚举属性息息相关, 同时取消vc.automaticallyAdjustsScrollViewInsets
属性值.
scrollView关键属性的实践(一)
vc在navigationController中, navigationBar是默认状态, 并且self.automaticallyAdjustsScrollViewInsets = YES
和scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
都是使用的默认值.(注意如果当前vc没有在navigationController或者TabbarController中时候, 结果可能不一样, 我们一般讨论的是scrollView在navigationController中的情况)
-(void)viewDidLoad{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scrollView];
scrollView.frame = self.view.bounds;
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
self.scrollView = scrollView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[scrollView addSubview:button];
button.frame = CGRectMake(0, 0, 100, 100);
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(buttonDidClick) forControlEvents:UIControlEventTouchUpInside];
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
} else {
self.automaticallyAdjustsScrollViewInsets = YES;
}
}
在iOS8上:
2018-09-27 14:49:13.103 scrollViewInVcDemo[56036:1691899] contentInset: {64, 0, 0, 0}
2018-09-27 14:49:13.103 scrollViewInVcDemo[56036:1691899] contentOffset: {0, -64}
在iOS11上:
2018-09-27 14:47:23.731497+0800 scrollViewInVcDemo[55894:1682631] contentInset: {0, 0, 0, 0}
2018-09-27 14:47:23.731605+0800 scrollViewInVcDemo[55894:1682631] contentOffset: {0, -64}
2018-09-27 14:47:23.731708+0800 scrollViewInVcDemo[55894:1682631] safeArea: {64, 0, 0, 0}
2018-09-27 14:47:23.731867+0800 scrollViewInVcDemo[55894:1682631] adjustedContentInset: {64, 0, 0, 0}
最终界面展示是一致的, 正方形的button 位于界面的左上方, navBar下:
上述代码中, 如果设置self.navigationController.navigationBar.hidden = YES;
, 得到的结果如下:
iOS8:
2018-09-27 15:16:04.581 scrollViewInVcDemo[57368:1792003] contentInset: {20, 0, 0, 0}
2018-09-27 15:16:04.581 scrollViewInVcDemo[57368:1792003] contentOffset: {0, -20}
iOS11:
2018-09-27 15:16:56.342570+0800 scrollViewInVcDemo[57444:1799639] contentInset: {0, 0, 0, 0}
2018-09-27 15:16:56.342685+0800 scrollViewInVcDemo[57444:1799639] contentOffset: {0, -20}
2018-09-27 15:16:56.342787+0800 scrollViewInVcDemo[57444:1799639] safeArea: {20, 0, 0, 0}
2018-09-27 15:16:56.342894+0800 scrollViewInVcDemo[57444:1799639] adjustedContentInset: {20, 0, 0, 0}
可以得出结论:
- iOS10以前通过设置
self.automaticallyAdjustsScrollViewInsets = YES;
可以改变scrollView的contentInset
, 从而使得scrollView上的元素不会被navigationBar或者tabbar遮挡. - 在iOS11以后.则通过设置
contentInsetAdjustmentBehavior
属性来改变adjustedContentInset
属性, 来完成相同效果, 同时contentInset
属性并未修改.并且automaticallyAdjustsScrollViewInsets
属性失效.
scrollView关键属性的实践(二)
如果我们设置如下关键属性:
self.navigationController.navigationBar.hidden = NO;
if (@available(iOS 11.0, *)) {
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
得到的结论如下:
/**
iOS 8:
2018-10-08 16:30:31.331 scrollViewInVcDemo[3018:165133] scrollView: <UIScrollView: 0x7fddcec31390; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fddcec32050>; layer = <CALayer: 0x7fddcec318c0>; contentOffset: {0, 0}; contentSize: {375, 1334}>
2018-10-08 16:30:31.331 scrollViewInVcDemo[3018:165133] contentInset: {0, 0, 0, 0}
2018-10-08 16:30:31.332 scrollViewInVcDemo[3018:165133] contentOffset: {0, 0}
iOS 11:
2018-10-08 16:36:40.084890+0800 scrollViewInVcDemo[3252:188595] scrollView: <UIScrollView: 0x7ffd2d837000; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000097a8b0>; layer = <CALayer: 0x6000007447a0>; contentOffset: {0, 0}; contentSize: {375, 1334}; adjustedContentInset: {0, 0, 0, 0}>
2018-10-08 16:36:40.085049+0800 scrollViewInVcDemo[3252:188595] contentInset: {0, 0, 0, 0}
2018-10-08 16:36:40.085251+0800 scrollViewInVcDemo[3252:188595] contentOffset: {0, 0}
2018-10-08 16:36:40.085379+0800 scrollViewInVcDemo[3252:188595] safeArea: {64, 0, 0, 0}
2018-10-08 16:36:40.085521+0800 scrollViewInVcDemo[3252:188595] adjustedContentInset: {0, 0, 0, 0}
*/
两者结果都如下图:
可以得出结论:
iOS10以前通过设置
self.automaticallyAdjustsScrollViewInsets = NO
;与在iOS11以后设置contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever
属性是等效的.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever
属性时候会有如下关系:adjustedContentInset == contentInset
iOS11以后, contentInsetAdjustmentBehavior
与adjustedContentInset
的关系
- UIScrollViewContentInsetAdjustmentAutomatic:
如果scrollview在一个automaticallyAdjustsScrollViewContentInset = YES
的controller上,并且这个Controller包含在一个navigationController中,这种情况下会设置在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
iOS中的安全区与additionalSafeAreaInsets
在https://www.jianshu.com/p/efbc8619d56b中提到:
在iOS 11以后 Controller的automaticallyAdjustsScrollViewInsets属性被废弃了,所以当tableView超出安全区域时系统自动调整了SafeAreaInsets值,进而影响adjustedContentInset值,在iOS 11中决定tableView的内容与边缘距离的是adjustedContentInset属性,而不是contentInset。adjustedContentInset的计算方式见本文第二部分内容。因为系统对adjustedContentInset值进行了调整,所以导致tableView的内容到边缘的距离发生了变化,导致tableView下移了20pt(statusbar高度)或64pt(navigationbar高度)。
如果你的APP中使用的是自定义的navigationbar,隐藏掉系统的navigationbar,并且tableView的frame为(0,0,SCREEN_WIDTH, SCREEN_HEIGHT)开始,那么系统会自动调整SafeAreaInsets值为(20,0,0,0),如果使用了系统的navigationbar,那么SafeAreaInsets值为(64,0,0,0),如果也使用了系统的tabbar,那么SafeAreaInsets值为(64,0,49,0)。关于什么情况下会发生内容下移的问题,本文第三部分有介绍。
在iOS7中Apple引入的UIViewController的topLayoutGuide
和bottomLayoutGuide
属性, 他们可以让我们创建约束已避免内容被UIKit的横条, 状态栏, 导航或者标签栏覆盖, 现在在iOS11中全部被被废弃, 并且由安全区
替代.
viewController在iOS11中添加了一个新的属性additionalSafeAreaInsets
, 通过该属性, 我们可以规定我们自己的safe ares insets, 最终系统根据我们设置的additionalSafeAreaInsets
以及 adjustedContentInset
, 系统会计算出最终的 safe area.
注意: 如果viewController是在
navigationController
中, 那么应该先修改NavigationController的additionalSafeAreaInsets
, 然后再改变viewController
的additionalSafeAreaInsets
.
建议仔细读一下下面的参考资料, 腾讯 bugly 写的文章非常棒