iPhone - Auto resizing UIWebView content do not fit the frame

For those, who faced same problem, you can turn off the UIWebView native scrolling by

self.webView.scrollView.scrollEnabled = NO;

and add a separate scrollView which will handle scrolling and zooming instead of webView's native scroll. Next implement

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
   CGRect frame = _webView.frame;
   CGSize fittingSize = [_webView sizeThatFits:_webView.scrollView.contentSize];
   frame.size = fittingSize;
   _webView.frame = frame;
   self.scrollView.contentSize = self.webView.scrollView.contentSize;
}

to set proper webView frame. Hope this helps someone. Good coding!


The answer is: you are already doing this but there are limits.

Look at the following screenshots:

Screeshot 1, scalesPageToFit YES and NO

Both webview have the same width. In the lower one the page fits perfectly.

enter image description here

Screeshot 2, scalesPageToFit both YES but smaller widths set

Both webview try to fit the page but it won't as there is a size limit.

enter image description here


Try this.That's working for me.

NSString *strTemplateHTML = [NSString stringWithFormat:@"<html><head><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body style='margin:0; padding:0;'>%@</body></html>", @"insert your html content here"];

[webView loadHTMLString:strTemplateHTML baseURL:nil];

Use sizethatfits() in the webview which would resolve your issue.