Scroll to top of UITableView by tapping status bar

2019-01-21 00:57发布

I know there's tons of code out there to scroll a tableview to the top, but I want to do this when the top status bar is tapped, just like in Apple's native apps. Is this possible?

9条回答
唯我独甜
2楼-- · 2019-01-21 01:13

I know this is quite an old one but hope this can help. Following what @MarkGranoff said, the scrollsToTop doesn't work if more than one UIScrollView, or its subclasses, has got it set to YES (default value), a sanity check is probably worth to check who's actually messing up with this behaviour. The simple method below loop over the subviews of your view and logs the scrollsToTop value of all the UIScrollView in your view. Preferably to be called in your viewDidAppear method.

- (void)checkForScrollViewInView:(UIView *)view {
    for (UIView *subview in [view subviews]) {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            NSLog(@"scrollsToTop enabled: %i in scroll view %@", ((UIScrollView *)subview).scrollsToTop, subview);
        }
        if (subview.subviews.count > 0) {
            [self checkForScrollViewInView:subview];
        }
    }
}

This is just a debug code indeed. Once you find the scrollsToTop value for each one of the UIScrollView subclasses just make sure only one is set to YES.

查看更多
欢心
3楼-- · 2019-01-21 01:16

Using the information given in other answers, I added the following code to my UITableViewController get it to work:

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (UITextView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITextView class]]) {
            view.scrollsToTop = NO;
        }
    }

    self.tableView.scrollsToTop = YES;
}

This looks through all the views in the UITableViewController's hierarchy and turns off scrollsToTop on all the UITextViews that were intercepting the touch event. Then, ensured the tableView was still going to receive the touch.

You can mod this to iterate through other UITableViews / UIScrollViews / UITextViews that may be intercepting as well.

Hope this helps!

查看更多
Anthone
4楼-- · 2019-01-21 01:17

I had the same problem but fixed by following steps:

  1. Set scrollsToTop = YES for tableview you wanted to scroll to top.
  2. set scrollsToTop = NO for all other tableview or collection view or scrollview.
  3. If any of your tableview cell has collection view . Make sure you set scrollsToTop to NO for the collection view as well.

If your view controller/ navigation controller is added as a subview on another view controller, Make sure you set it as a child Controller.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-21 01:24

On UIScrollView header file:

// 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.

查看更多
Fickle 薄情
6楼-- · 2019-01-21 01:25

For example if you have a table view and scroll view like tags like this

enter image description here

you should make something like this in viewDidLoad

self.tableView.scrollsToTop = true
self.tagsView.scrollsToTop = false
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-21 01:25

There can be multiple UIScrollView descendants loaded eg.: having a UIPageViewcontroller on each page containing UITableViews.

The scrollsToTop property is true by default.

So in addition to handling nested UIScrollViews' scrollsToTop property, you should do the following:

//When the view is loaded disable scrollsToTop, this view may not be the visible one
override func viewDidLoad() {
    super.viewDidLoad()
    ...
    tableView.scrollsToTop = false
}

//Now it's time to enable scrolling, the view is guaranteed to be visible
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    tableView.scrollsToTop = true
}

//Do not forget to disable scrollsToTop, making other visible UIScrollView descendant be able to be scrolled to top
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.tableView.scrollsToTop = false
}

This way only one top level UITableView's scrollsToTop will be set to true.

查看更多
登录 后发表回答