Scroll to top of UITableView by tapping status bar
Solution 1:
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:
If you came from Google and need a complete checklist:
- Check that you've set scrollsToTop=YES (per Mark's suggestion) on your UITableView
- 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...
- 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:
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:
I had the same problem but fixed by following steps:
- Set scrollsToTop = YES for tableview you wanted to scroll to top.
- set scrollsToTop = NO for all other tableview or collection view or scrollview.
- 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.