Search code examples
iphoneuiwebviewuiscrollviewuitextviewautoresize

UIWebView - Autoresize so that no scrolling (in the webView itself) is needed


I'm trying to use a UIWebView for displaying content higher than the screen of the iPhone, without needing to scroll in the UIWebView itself.

The UIWebView is placed as a subview to a UIScrollView, along with some other objects that I want the UIScrollView to scroll up and down with the UIWebView.

I know you can do this with a UITextView like this:

CGRect frame = _textView.frame; frame.size.height = _textView.contentSize.height; _textView.frame = frame;

But the UIWebView does not inherit from UIScrollView and does therefore not contain the contentSize-property.

I'd really like to keep it a UIWebView, because the data I get is in HTML-blocks.

Thank you!


Solution

  • I think the only way you'll be able to do this is to use some javascript to get the size of the web page and adjust the size of the UIWebView control.

    Something like the following should do the trick

    int content_height = [[theWebView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"] integerValue];
    CGRect rect = theWebView.frame;
    rect.size.height = content_height;
    theWebView.frame = rect;
    

    You may need to add some sort of fudge factor to the content height.