1

This is an update to a earlier question. Once I load up a UIWebView with some string based HTML content, is there a way to determine if the view would require scrolling to see the entirety of the content? I am looking for some sort of flag or way of knowing if content is below the bottom of the UIWebView.

Thanks in advance!

Rob Bonner
  • 9,276
  • 8
  • 35
  • 55

2 Answers2

1

You could subclass UIWebView and use the following initalizers:

-(id) initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        for (UIView* v in self.subviews){
            if ([v isKindOfClass:[UIScrollView class]]){
                self.scrollview = (UIScrollView*)v; 
                break;
            }
        }
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame])
    {
        for (UIView* v in self.subviews){
            if ([v isKindOfClass:[UIScrollView class]]){
                self.scrollview = (UIScrollView*)v; 
                break;
            }
        }
    }
    return self;
}

then have a property called

@property (nonatomic, retain) UIScrollView *scrollview;

You then have access to the scrollview within the UIWebView and can check its content size to see if it is bigger than your views size

JFoulkes
  • 2,429
  • 1
  • 21
  • 24
  • This is kinda working, great idea. I did the check like this: (w being my subclassed webview) [w loadHTMLString:content baseURL:url]; [scrollIssue addSubview:w]; CGRect rect = w.frame; UIScrollView *sv = [w scrollview]; CGSize rectv = sv.contentSize; They are always the same even when it needs to scroll. Suspecting it is not painted, is there a way to force it to layout prior to the check? – Rob Bonner Mar 29 '11 at 20:47
  • could try a call to layoutSubviews on the webview? – JFoulkes Mar 30 '11 at 08:17
0

The simplest way is probably to use Javascript to get the document height:

NSInteger height = [[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] intValue];

(haven't actually tested this so YMMV. Different browsers put the document height in different objects and properties and I can't remember which one works in Webkit... see How to get height of entire document with JavaScript?)

If the height returned is greater than the UIWebView height it's going to need scrolling.

Community
  • 1
  • 1
Euan
  • 3,953
  • 1
  • 19
  • 15