Luke Luke - 5 months ago 26
iOS Question

How to trigger scrollsToTop when UITableView is nested inside UIScrollView

I was wondering if anyone had had any luck triggering scrollsToTop (or by any other means) for a UITableView from the user tapping on the status bar when the UITableView is nested inside a UIScrollView that also observes this function? I know this probably isn't good practice, but in this instance the UX kind of calls for this type of hierarchy.

Either way, I've seen a whole bunch of proposals ranging from private methods (obviously not going to happen) to adding fake windows over the status bar (also not going to happen).

Answer

Ok, so the answer here is two fold:

  1. You cannot have more than one UIScrollView (or classes deriving from or using UIScrollView - i.e. UITableView) on the same UIView with the property scrollsToTop set to YES. Pick the one you want to have the feature and make sure all others are no

    For example, do this:

    scrollView.scrollsToTop = NO;
    tableView.scrollsToTop = YES; // or not set
    
  2. Implement the UIScrollView delegate method scrollViewShouldScrollToTop: and return YES if the calling UIScrollView is the UITableView.

Props to this answer for mentioning the non-multiple scrollsToTop option.

Comments