Scroll to top of UITableView by tapping status bar

IosObjective CScrollTableviewStatusbar

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

You get this for free, but you should check that the scrollsToTop attribute of your UITableView is YES.

When this does NOT work is when you have a UIScrollView (or descendant class like UITextView) object embedded inside another UIScrollView class (like UITableView). In this case, set scrollsToTop on the embedded UIScrollView class to NO. Then the tap-the-status-bar behavior will work.

Solution 2 - Ios

If you came from Google and need a complete checklist:

  1. Check that you've set scrollsToTop=YES (per Mark's suggestion) on your UITableView
  2. Make sure that you've set scrollsToTop=NO on all OTHER UITableViews / UIScrollViews / UITextViews in your window, so that they're not intercepting the click. I've found myself printing out all the views in my window many times to debug this...
  3. Make sure that your table view is at 0/0 (x/y coordinates) within the window - this is how the system knows that it should pass the message

Solution 3 - Ios

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!

Solution 4 - Ios

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.

Solution 5 - Ios

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.

Solution 6 - Ios

Like Mark said, you can only have one subclass of UIScrollView (usually the table view) that has the scrollsToTop property set to TRUE. Likely you have others, typically UITextView in your view. Just set their scrollsToTop property to FALSE and you're good to go.

Solution 7 - Ios

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.

Solution 8 - Ios

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

Solution 9 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniOSDevView Question on Stackoverflow
Solution 1 - IosMark GranoffView Answer on Stackoverflow
Solution 2 - IosZane ClaesView Answer on Stackoverflow
Solution 3 - IosBrandon RobertsView Answer on Stackoverflow
Solution 4 - IosiOS DeveloperView Answer on Stackoverflow
Solution 5 - IosDennyLouView Answer on Stackoverflow
Solution 6 - IosGeorgeView Answer on Stackoverflow
Solution 7 - IosfoskonView Answer on Stackoverflow
Solution 8 - IosAtefView Answer on Stackoverflow
Solution 9 - IosGéza MiklóView Answer on Stackoverflow