iPhone - Auto resizing UIWebView content do not fit the frame

Oliver picture Oliver · Apr 23, 2011 · Viewed 69.7k times · Source

I'm generating an UIWebView into my viewDidLoad method, with a tiny size (let's say something like 50x70). And then I put it into a super UIView.

I'd like to make its content fit the webView frame. To do this, I wrote :

        oneView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, W, H)];
        oneView.backgroundColor = [UIColor whiteColor];
        oneView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        oneView.scalesPageToFit = YES;
        oneView.autoresizesSubviews = YES;

        [self.view addSubview:oneView];
        [oneView loadRequest:/*some request*/];

But doing this, the web page is not resized to the frame of the UIWebView. It seems to be scaled to something else, smaller than the superview's frame, and wider than the webview's one.

enter image description here

If I set the webview frame size to the whole superview's size, it's ok.
How may I force the web content (real www content) to fit the frame of the "reduced" UIWebView ?

Answer

Nick Weaver picture Nick Weaver · Apr 23, 2011

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