Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic scroll to top doesn't work in UITableView

usually when tapping the top of the screen the tableview scrolls all the way to the top. For some reason this doesn't work in one of my view controllers.

The hirarchy is as follows:

UIView
-> WebView
-> TableView
----->SearchBar
SearchDisplayController.

I think I have everything hooked up correctly (datasource, delegate, ...). I have a similar view controller where everything works. The only difference seems to be WebView, which is missing in the view controller where the tap-and-scroll-to-top works...

Any ideas?

Best regards, Sascha

like image 603
Sascha Avatar asked Nov 12 '10 18:11

Sascha


People also ask

How to scroll to top of UITableView Swift?

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.

Is UITableView scrollable?

UITableView is a subclass of UIScrollView that allows users to scroll the table vertically (the closely-related UICollectionView class allows for horizontal scrolling and complex two-dimensional layouts).

How do I scroll programmatically in Swift?

If you want to programmatically make SwiftUI's ScrollView move to a specific location, you should embed a ScrollViewReader inside it. This provides a scrollTo() method that can move to any view inside the parent scrollview, just by providing its anchor.

How do I scroll to the bottom of UITableView?

To scroll to the bottom of the table view items, use ScrollToRow. Three parameters are required for this: NSIndexPath: Specify which row item to scroll to.


2 Answers

You have probably added some view to your view hierarchy that is a UIScrollView or contains scroll views (e.g. UIWebView or UITextView). When you tap the status bar, iOS searches for the topmost scrollview with scrollsToTop set to YES and scrolls to its top.

Add this to your class:

- (void) disableScrollsToTopPropertyOnAllSubviewsOf:(UIView *)view {
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)subview).scrollsToTop = NO;
        }
        [self disableScrollsToTopPropertyOnAllSubviewsOf:subview];
    }
}

…and call

[self disableScrollsToTopPropertyOnAllSubviewsOf:myAddedSubview];

to solve the problem and let your table view scroll to top again.

like image 185
opyh Avatar answered Oct 15 '22 07:10

opyh


You need to disable scrollsToTop on all but one of the scrollable views. Since UITableView is a descendant of UIScrollView, it's easy to do. However, if you want to disable it on the webview, it's a little trickier. This seems to work:

[webView.subviews objectAtIndex:0].scrollsToTop = NO;

But that is a little iffy since it's poking around in undocumented parts of the webview. It may stop working if Apple decides to rearrange the subviews.

like image 32
Brian Avatar answered Oct 15 '22 06:10

Brian