TableView,点击状态栏滚动到顶部
背景
- iPhone上的应用,基本上都有个特点,只要有表格,那么用户点击状态栏,应该就会自动滚动到顶部,在下拉就可以获取最新数据了。如网易新闻、百思不得姐、糗事百科等等。
为什么能滚动?
- 既然能滚动,说明它是个scrollView(当然,tableView也是scrollView,废话了)
什么情况下,默认就会滚动?
- 控制器上只有一个scrollView,原因:
// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
@property(nonatomic) BOOL scrollsToTop __TVOS_PROHIBITED; // default is YES.
什么情况下,默认不会滚动?
// 引用上面苹果官方文档注释内容
If more than one is found, none will be scrolled
// 也就是说:每个scrollView的scrollsToTop属性默认都是YES,当有多个scrollView时,点击状态栏都不会滚动到顶部了!
不会滚动时,怎么才能让它滚动?
- 从苹果官方文档中可以学习到,点击状态栏自动滚动的前提是:
- 1.只有一个scrollView
- 2.有多个scrollView,但只有一个scrollView的scrollsToTop属性为YES!
多个scrollView下,自动滚动示例分析
-
界面效果图:
TableView,点击状态栏滚动到顶部-效果图.png
-
结构分析
- 顶部:存放标签的scrollView
- 内容:存放tableView的scrollView
-
滚动设置
-(void)viewDidLoad {
[super viewDidLoad];
// 把其它不需要滚动到顶部的scrollView的scrollsToTop设置成NO
self.titlesScrollView.scrollsToTop = NO;
self.contentsScrollView.scrollsToTop = NO;
}
#pragma mark - 设置当前显示的tableView点击状态栏返回到顶部
// 把当前显示的tableView的scrollsToTop属性设置成YES,其它设置成NO
-(void)setupScrollToTopWithCurrentDisplayVc:(UIViewController *)vc {
for (UIViewController *tvc in self.childViewControllers) {
if ([tvc isKindOfClass:BaseListViewController.class]) {
BaseListViewController *tv = (BaseListViewController *)tvc;
tv.listView.scrollsToTop = NO;
}
}
if ([vc isKindOfClass:BaseListViewController.class]) {
BaseListViewController *tv = (BaseListViewController *)vc;
tv.listView.scrollsToTop = YES;
}
}
网友评论