Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Auto resizing UIWebView content do not fit the frame

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 ?

like image 994
Oliver Avatar asked Apr 23 '11 11:04

Oliver


2 Answers

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

like image 113
Nick Weaver Avatar answered Sep 23 '22 21:09

Nick Weaver


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]; 
like image 42
Rajni Johar Raswant Avatar answered Sep 22 '22 21:09

Rajni Johar Raswant