I inserted a UITableView
inside another UIViewController
's view.
but when I tap the status bar at the top of the screen; the table view doesn't scroll to the top which is the expected behavior within iOS apps.
I tried :
[self.tableView setScrollsToTop:YES];
but the tableview still not scroll to top when tap the top of the screen.
iPhone has had this feature for a long time, but many users aren't aware of it. You can tap the clock in the status bar to scroll to top in any app on your iPhone. If you have a iPhone X or newer devices, the ones with a “notch”, you can tap on either sides of the notch to scroll to the top.
To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.
UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.
Since a scrollView has a panGesture we can check the velocity of that gesture. If the tableView was programmatically scrolled the velocity in both x and y directions is 0.0. By checking this velocity we can determine if the user scrolled the tableView because the panGesture has a velocity.
You have to set scrollsToTop
to NO
on all the other scroll views in your hierarchy. If more than one scroll view has scrollsToTop
set to YES
, none of them will scroll to top when the status bar is touched. This is mentioned in the documentation of the scrollsToTop
property.
Keep in mind that this also applies to any subclasses of UIScrollView
in your view hierarchy, and that UITableView
, UIContainerView
, and UITextView
are subclasses of UIScrollView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With