UITableView's, UIWebViews and the scrollsToTop

2019-04-28 23:55发布

My app has a UITableView. That UITableView has a header view, which is a UIWebView.

By default, scroll views have their scrollsToTop property set to YES, which will enable the user to tap the status bar to scroll to the top of the scroll view.

When there are two scroll views embedded in one view, that both have their scrollsToTop property set to YES, tapping the status bar does nothing.

The solution is to set one of the scrollsToTop properties to NO. That re-enables tapping the status bar.

Now here's the problem: UIWebView doesn't expose it's scroll view, and as a result, there is no access to it's scrollsToTop property. I only want the table view to scroll to the top when the status bar is tapped, not the web view.

Does anyone know how I can achieve this?

4条回答
相关推荐>>
2楼-- · 2019-04-28 23:59

There's now a scrollView property, which is preferred:

myWebView.scrollView.scrollsToTop = YES;
查看更多
Evening l夕情丶
3楼-- · 2019-04-29 00:06

Have you tried the delegate method from UIScrollViewDelegate scrollViewShouldScrollToTop?

查看更多
混吃等死
4楼-- · 2019-04-29 00:09

This question contains the answer:

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

I couldn't find it previously since it was asked differently and on a slightly different topic, but the result is the same.

So the outcome was to walk through the subviews until you find the scroll view. I've done this before in other apps and didn't think of it in this case. It should be App-Store-Safe, as I have apps on the store that use this idea.

A category on UIWebView to enable or disable scrolling to top:

@implementation UIWebView (UIWebViewScrollToTopAdditions)

- (void)setScrollsToTop:(BOOL)scrollsToTop
{
    if ([[self subviews] count] > 0)
    {
        UIScrollView *scrollView = (UIScrollView *)[[self subviews] objectAtIndex:0];
        if ([scrollView respondsToSelector:@selector(setScrollsToTop:)])
        {
            [scrollView setScrollsToTop:scrollsToTop];
        }
    }
}

@end
查看更多
Anthone
5楼-- · 2019-04-29 00:09

This often happens because all scrollviews have a default value. When new scrollviews or webviews are created they will have scrollsToTop set to YES. I've written a simple category for this specific issue:

-[UIScrollView makeOnlyThisScrollViewScrollToTopOnStatusBarTap];

https://gist.github.com/hfossli/6776203

It basically sets scrollsToTop to NO on all other scrollViews than the one you are specifying + taking care of the default value. Good luck!

查看更多
登录 后发表回答