iPhone OS: Tap status bar to scroll to top doesn't work after remove/add back

半城伤御伤魂 提交于 2019-11-27 20:18:14

You could try setting the ScrollsToTop property to true again after re-showing it:

[currentView setScrollsToTop:YES];

If that's not working, are you definitely only showing one view? If there is more than one scrolling view a scrollViewDidScrollToTop message is ignored...

In iOS 5.0 you can access the scrollview property of the UIWebView

webView.scrollView.scrollsToTop = YES;
user305578

The following fix by Alex worked for me. Thanks!

((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = NO;

Being in a hurry this fix worked great, however given more time I might've subclassed the UIWebView and accessed the protected UIScrollView member directly.

The worry I have with Alex' method is that it assumes that UIScrollView is at index zero of the subviews (encapsulation allows private members to change). Which suggests another solution still:

for (UIView* v in [webView subviews])
{
    if ([v isKindOfClass:[UIScrollView class]])
    {
        (UIScrollView *)v.scrollsToTop = NO;
    }
}

I was having a similar problem where the scroll-to-top functionality was lost. Turns out this will only work when you have only one active view at a time (within the same scroll view). In my case I had a table view and another view which would fade in/out. Adding a removeFromSuperview at the end of the animation did the trick.

The answer was in the UIScrollView.h file comments:

/*
 this is for the scroll to top gesture. by default, a single scroll visible scroll view with this flag set will get the call. if there is more than one visible with this
 flag set or the delegeat method returns NO, the view isn't scrolled 
 */
@property(nonatomic) BOOL  scrollsToTop;          // default is YES. if set, special gesture will scroll to top of view after consulting delegate
Nick Toumpelis

You can use the following code to have the UIWebView ignore scrollToTop without the extra UIScrollView:

((UIScrollView *)[[webView valueForKey:@"_internal"] valueForKey:@"scroller"]).scrollsToTop = NO;

I had a similar problem after playing a Youtube video within my app. scrollsToTop was still set to YES but tapping the status bar had no effect.

I finally realised that my app window was no longer the key window. After adding the following line to a UIWindow subclass (which I already had for other reasons) everything worked as it should again:

if (![self isKeyWindow]) [self makeKeyWindow];

I just ran across a similar behavior in the app I'm currently working on. In its case, if you load a YouTube video from within a UIWebView, scroll to top stops working for the rest of the application's life cycle. I kind of assume this might happen after loading the movie player as well, but haven't confirmed. That functionality has been around a lot longer and probably has fewer bugs.

lansher1985

When there are multiple scrollview, you can also set scrollUpToTop to NO for the others scrollview. cf:

setScrollsToTop with multiple UIScrollView classes and/or subclasses(UITableView)

I want to add my case, I add an UIWebView on an UIScrollView, as h4xxr had answered on the top:

If there is more than one scrolling view a scrollViewDidScrollToTop message is ignored

So, I get a simply way to make it work on webView: just set the scrollView·s scrollsToTop property false.

And when tap the status bar, it won`t got intercepted by the scrollView, and the webView scrolls to the top!

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.scrollsToTop = false; //igore scrollView`s scrollsToTop
    [self.view addSubview:scrollView];

    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = scrollView.bounds;
    [scrollView addSubview:webView];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!