开发者

UITableView's, UIWebViews and the scrollsToTop Property = Trouble

开发者 https://www.devze.com 2022-12-28 10:10 出处:网络
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

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: UIWeb开发者_JAVA技巧View 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?


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

myWebView.scrollView.scrollsToTop = YES;


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


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!


Have you tried the delegate method from UIScrollViewDelegate scrollViewShouldScrollToTop?

0

精彩评论

暂无评论...
验证码 换一张
取 消