日常收录

TableView,点击状态栏滚动到顶部

2016-04-06  本文已影响1476人  chernyog

TableView,点击状态栏滚动到顶部

背景

为什么能滚动?

什么情况下,默认就会滚动?

// 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时,点击状态栏都不会滚动到顶部了!

不会滚动时,怎么才能让它滚动?

多个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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读