0

I need to detect if the scroll bar is visible in the UIWebView, how do I do this? My first attempt was:

int scrollHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];
if(scrollHeight > [webView frame].size.height)
    NSLog(@"Scrollbar is visible");

But the scrollHeight is always a way larger than the webView height.

cocoapriest
  • 1,869
  • 3
  • 22
  • 36
  • Ask yourself why you need to know when it's visible. This smells as though you're doing something wrong. – jer Oct 28 '10 at 17:58
  • No, there's nothing wrong. It's the client's wish to visually inform the user when he can/should scroll. – cocoapriest Oct 28 '10 at 18:04
  • Maybe you could use the [scrollView flashScrollIndicators] method to bling the scrollbars and notify your users that way? – texmex5 Oct 28 '10 at 18:15
  • @Konstantin, Clients often what things they cannot have. Your job is to give them what they ask for of what is possible. UIWebView is not a UIScrollView. Period, full stop. – jer Oct 28 '10 at 19:36
  • This is not always a solution to hard problems – cocoapriest Oct 28 '10 at 19:59

2 Answers2

1

Another possible solution is to subclass UIWebView and implement the following:

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    scrollView.bounces = NO;
}

The "shadow" will not be visible anymore, because bouncing is not possible anymore...

jott-ess
  • 11
  • 1
0

Try the following (no private APIs):

for (id subView in [webView subviews]) {
    if ([subView respondsToSelector:@selector(flashScrollIndicators)]) {
        [subView flashScrollIndicators];
    }
}

This doesn't assume anything about the internal UIWebView hierarchy, but just if there happens to be a subview that responds to flashScrollIndicators they will be flashed. ;)

Matthew Leffler
  • 1,386
  • 1
  • 19
  • 36
  • yes, I've seen this before, but I need to create a custom way to visualize this (not by flashScrollIndicators) – cocoapriest Oct 28 '10 at 19:21
  • Check this out: http://stackoverflow.com/questions/745160/how-to-determine-uiwebview-height-based-on-content-within-a-variable-height-uita/751326#751326 – Matthew Leffler Oct 28 '10 at 19:36