自动滚动到顶部在UITableView中不起作用

时间:2021-01-18 21:12:52

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:

hirarchy如下:

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

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...

我想我已经把所有东西都搞定了(datasource,delegate,...)。我有一个类似的视图控制器,一切正常。唯一的区别似乎是WebView,它在视图控制器中缺失,其中tap-and-scroll-to-top工作...

Any ideas?

有任何想法吗?

Best regards, Sascha

此致,Sascha

8 个解决方案

#1


27  

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:

您需要在除一个可滚动视图之外的所有视图上禁用scrollsToTop。由于UITableView是UIScrollView的后代,因此很容易做到。但是,如果要在webview上禁用它,则有点棘手。这似乎有效:

[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.

但这有点不确定,因为它在webview的未记录部分中徘徊。如果Apple决定重新安排子视图,它可能会停止工作。

#2


30  

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.

您可能已向视图层次结构添加了一些视图,该视图层次结构是UIScrollView或包含滚动视图(例如UIWebView或UITextView)。当您点击状态栏时,iOS将搜索最顶层的scrollview,并将scrollsToTop设置为YES并滚动到顶部。

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.

解决问题,让您的表视图再次滚动到顶部。

#3


4  

Don't know what Apple were smoking but by design, the behaviour on iPhone is distinctly different from iPad. Apple's documentation does include a "Special Condsideration" note as follows...

不知道Apple吸烟的是什么,但按照设计,iPhone上的行为与iPad截然不同。 Apple的文档确实包括如下的“特殊情况”说明......

On iPhone, the scroll-to-top gesture has no effect if there is more than one scroll view on-screen that has scrollsToTop set to YES.

在iPhone上,如果屏幕上有多个滚动视图,其中scrollsToTop设置为YES,则滚动到顶部的手势无效。

So on iPhone you have to ensure that there is only ever one UIScrollView (or UITableView/UICollectionView/UIWebView etc) that has its scrollsToTop = YES.

所以在iPhone上你必须确保只有一个UIScrollView(或UITableView / UICollectionView / UIWebView等),它的scrollsToTop = YES。

Since scrollsToTop defaults to YES you have to explicitly set scrollsToTop = NO for the UIScrollView's you do not want to scroll when the user taps the status bar.

由于scrollsToTop默认为YES,因此您必须为UIScrollView显式设置scrollsToTop = NO,当用户点击状态栏时您不想滚动。

#4


3  

Apparently if you have more than one scrolling view (in your case the WebView and TableView) in a view controller, the "tap status bar to scroll to top" is disabled.

显然,如果在视图控制器中有多个滚动视图(在您的情况下是WebView和TableView),则“点击状态栏以滚动到顶部”将被禁用。

http://twitter.com/drance/status/2448035250438144

http://twitter.com/drance/status/24480352504381​​44

(Matt Drance is a former iPhoneOS developer evangelist and current iOS development rockstar)

(Matt Drance是前iPhoneOS开发者传播者和当前iOS开发摇滚明星)

#5


3  

Swift variant of opyh's answer. Upvote him!

Swyh的答案的快速变体。支持他!

func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
    for subview in view.subviews {
        if let scrollView = subview as? UIScrollView {
            (scrollView as UIScrollView).scrollsToTop = false
        }
        self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
    }
}

#6


1  

I just wanted to add to this, if you are using a UITextView, it isn't the subviews that need the scrollsToTop adjusting, it's the actual UITextView, in short you need this code:

我只是想补充一点,如果你使用的是UITextView,那不是需要scrollsToTop调整的子视图,它是实际的UITextView,简而言之你需要这个代码:

[myTextView setScrollsToTop:FALSE];

This should fix the problem (additionally, the 'disableScrollsToTopPropertyOnAllSubviewsOf' function doesn't work on the UITextViews).

这应该可以解决问题(此外,'disableScrollsToTopPropertyOnAllSubviewsOf'功能在UITextViews上不起作用)。

Hope this helps someone!

希望这有助于某人!

#7


0  

I had this problem with just a tableview in the NIB. Very frustrating until I remembered I was programmatically adding a UITextView as the table view header. scrollsToTop=NO on the UITextView did the trick.

我在NIB中只有一个tableview就遇到了这个问题。非常令人沮丧,直到我记得我是以编程方式添加UITextView作为表视图标题。 UITextView上的scrollsToTop = NO就可以了。

#8


0  

Set scrollsToTop = NO on all scroll views in the view, except for the one you want to scroll to top. That helps the system find the correct scroll view.

在视图中的所有滚动视图上设置scrollsToTop = NO,但要滚动到顶部的滚动视图除外。这有助于系统找到正确的滚动视图。

#1


27  

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:

您需要在除一个可滚动视图之外的所有视图上禁用scrollsToTop。由于UITableView是UIScrollView的后代,因此很容易做到。但是,如果要在webview上禁用它,则有点棘手。这似乎有效:

[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.

但这有点不确定,因为它在webview的未记录部分中徘徊。如果Apple决定重新安排子视图,它可能会停止工作。

#2


30  

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.

您可能已向视图层次结构添加了一些视图,该视图层次结构是UIScrollView或包含滚动视图(例如UIWebView或UITextView)。当您点击状态栏时,iOS将搜索最顶层的scrollview,并将scrollsToTop设置为YES并滚动到顶部。

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.

解决问题,让您的表视图再次滚动到顶部。

#3


4  

Don't know what Apple were smoking but by design, the behaviour on iPhone is distinctly different from iPad. Apple's documentation does include a "Special Condsideration" note as follows...

不知道Apple吸烟的是什么,但按照设计,iPhone上的行为与iPad截然不同。 Apple的文档确实包括如下的“特殊情况”说明......

On iPhone, the scroll-to-top gesture has no effect if there is more than one scroll view on-screen that has scrollsToTop set to YES.

在iPhone上,如果屏幕上有多个滚动视图,其中scrollsToTop设置为YES,则滚动到顶部的手势无效。

So on iPhone you have to ensure that there is only ever one UIScrollView (or UITableView/UICollectionView/UIWebView etc) that has its scrollsToTop = YES.

所以在iPhone上你必须确保只有一个UIScrollView(或UITableView / UICollectionView / UIWebView等),它的scrollsToTop = YES。

Since scrollsToTop defaults to YES you have to explicitly set scrollsToTop = NO for the UIScrollView's you do not want to scroll when the user taps the status bar.

由于scrollsToTop默认为YES,因此您必须为UIScrollView显式设置scrollsToTop = NO,当用户点击状态栏时您不想滚动。

#4


3  

Apparently if you have more than one scrolling view (in your case the WebView and TableView) in a view controller, the "tap status bar to scroll to top" is disabled.

显然,如果在视图控制器中有多个滚动视图(在您的情况下是WebView和TableView),则“点击状态栏以滚动到顶部”将被禁用。

http://twitter.com/drance/status/2448035250438144

http://twitter.com/drance/status/24480352504381​​44

(Matt Drance is a former iPhoneOS developer evangelist and current iOS development rockstar)

(Matt Drance是前iPhoneOS开发者传播者和当前iOS开发摇滚明星)

#5


3  

Swift variant of opyh's answer. Upvote him!

Swyh的答案的快速变体。支持他!

func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
    for subview in view.subviews {
        if let scrollView = subview as? UIScrollView {
            (scrollView as UIScrollView).scrollsToTop = false
        }
        self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
    }
}

#6


1  

I just wanted to add to this, if you are using a UITextView, it isn't the subviews that need the scrollsToTop adjusting, it's the actual UITextView, in short you need this code:

我只是想补充一点,如果你使用的是UITextView,那不是需要scrollsToTop调整的子视图,它是实际的UITextView,简而言之你需要这个代码:

[myTextView setScrollsToTop:FALSE];

This should fix the problem (additionally, the 'disableScrollsToTopPropertyOnAllSubviewsOf' function doesn't work on the UITextViews).

这应该可以解决问题(此外,'disableScrollsToTopPropertyOnAllSubviewsOf'功能在UITextViews上不起作用)。

Hope this helps someone!

希望这有助于某人!

#7


0  

I had this problem with just a tableview in the NIB. Very frustrating until I remembered I was programmatically adding a UITextView as the table view header. scrollsToTop=NO on the UITextView did the trick.

我在NIB中只有一个tableview就遇到了这个问题。非常令人沮丧,直到我记得我是以编程方式添加UITextView作为表视图标题。 UITextView上的scrollsToTop = NO就可以了。

#8


0  

Set scrollsToTop = NO on all scroll views in the view, except for the one you want to scroll to top. That helps the system find the correct scroll view.

在视图中的所有滚动视图上设置scrollsToTop = NO,但要滚动到顶部的滚动视图除外。这有助于系统找到正确的滚动视图。