iPhone - Auto resizing UIWebView content do not fit the frame

*爱你&永不变心* 提交于 2019-11-29 22:16:21

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.

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.

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!

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];

You can do the following:

oneview.contentMode = UIViewContentModeScaleAspectFit;

That will make the content of your view grow as your view grows. The content will grow as big as possible while still fitting within the UIWebView and without distortion.

There are other options for this property, and you will find a pretty good explanation of the more complicated ones, along with pictures showing the effect of each, in the View Programming Guide for iOS

Note that the autoresizing mask you set will make the view itself grow only when its superview grows. If self.view is already big when the small UIWebView is created and self.view does not grow, the UIWebView won't be growing. You're probably aware of this, but I'm adding it just in case, since we can't see self.view's frame in this snippet of code.

Hope this is helpful.

For Swift 2.2 I have achieved the same with

//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")

let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)

For Swift 3, I have achieved the same with

DispatchQueue.main.async {
                if let finalURL = URL(string: urlString) {
                    let request = URLRequest(url: finalURL)

                    // self.webView.sizeToFit()
                    self.webView.scalesPageToFit = true
                    self.webView.contentMode = .scaleAspectFit
                    self.webView.loadRequest(request)
                }
            }
Pavel Kanapatski

Try this:

[oneView setScalesPageToFit:YES];

... instead of:

oneView.scalesPageToFit = YES;

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!