[ACCEPTED]-UIWebView does not scale content to fit-objective-c

Accepted answer
Score: 36

iOS5 gives a better solution

call this from 1 webViewDidFinishLoad

- (void)zoomToFit
{ 
    if ([theWebView respondsToSelector:@selector(scrollView)])
    {
        UIScrollView *scrollView = [theWebView scrollView];

        float zoom = theWebView.bounds.size.width / scrollView.contentSize.width;
        scrollView.minimumZoomScale = zoom;
        [scrollView setZoomScale:zoom animated:YES];
    }
}
Score: 30

I have subsequently discovered that some 4 web pages are correctly scaled, but some 3 are not. For web pages which are not properly 2 scaled, javascript can be used to control 1 zooming as follows:

NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Score: 23

You can use the below code:

self.webView.scalesPageToFit = YES;
self.webView.contentMode = UIViewContentModeScaleAspectFit;

in - (void)webViewDidFinishLoad:(UIWebView *)webView method or viewDidLoad

0

Score: 16

If you have access to the html you are loading 2 you can chuck the following meta tag in 1 the header :

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
Score: 10

in Swift2

self.webView.scalesPageToFit = true
self.webView.contentMode = UIViewContentMode.ScaleAspectFit

0

Score: 2

My solution in swift:

"Scales pages to fit" on 2 webView needs to be checked in interface 1 builder.

Use UIWebViewDelegate.

func webViewDidFinishLoad(webView: UIWebView) {
    let zoom = webView.bounds.size.width / webView.scrollView.contentSize.width
    webView.scrollView.setZoomScale(zoom, animated: true)
}
Score: 2

As mprivate mentions here you can update the zoom level 3 of the UIWebView.

- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
  CGSize contentSize = theWebView.scrollView.contentSize;
  CGSize viewSize = theWebView.bounds.size;

  float rw = viewSize.width / contentSize.width;

  theWebView.scrollView.minimumZoomScale = rw;
  theWebView.scrollView.maximumZoomScale = rw;
  theWebView.scrollView.zoomScale = rw;  
}

Another way of achieving 2 this would be injecting js code on the webViewDidFinishLoad 1 as RunLoop says.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
  CGFloat scale = 0.8; // the scale factor that works for you 
  NSString *js = [NSString stringWithFormat:@"document.body.style.zoom = %f;",scale];
  [webView stringByEvaluatingJavaScriptFromString:js];
}
Score: 2

Anybody looking for WKWebView answer, please try 2 the below code:

extension YourViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
        webView.evaluateJavaScript(jscript)
    }
}

Remember to set the navigationDelegate of the 1 Webview.

Score: 1

For Swift 3, using RunLoop's effective answer:

self.webView.stringByEvaluatingJavaScript(from: "document.body.style.zoom = 1.5;")

0

Score: 1

In xCode 8.3.3, go to the storyboard of 4 the view/scene where the web view is located 3 and check Scale Page to Fit in the attributes 2 inspector section . Also select Scale to 1 Fill as the content mode.

More Related questions