UIWebView set initial zoom scale programmatically when loading an external website?

29,376

Solution 1

Try this for setting the initial zoom level -

[webView stringByEvaluatingJavaScriptFromString:@"document. body.style.zoom = 5.0;"];

Also dont forget to set scalesPageToFit to NO and you're done.

If you set this to YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.

Solution 2

If you want to set initial zoom for your web view and then your web view can scaleable. You can try to add meta tag into the head tag of your HTML string at webViewDidFinishLoad protocol method. You can change 'initial-scale', 'minimum-scale'and 'maximum-scale' to adapt to your required. Looks like this:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString* js =
    @"var meta = document.createElement('meta'); " \
    "meta.setAttribute( 'name', 'viewport' ); " \
    "meta.setAttribute( 'content', 'width = device-width, initial-scale = 1.0,minimum-scale=1.0,maximum-scale=10.0 user-scalable = yes' ); " \
    "document.getElementsByTagName('head')[0].appendChild(meta)";
    [webView stringByEvaluatingJavaScriptFromString: js];
}

Solution 3

Why not:

   [self.webView.scrollView setZoomScale:2.0 animated:YES];
Share:
29,376
William Jockusch
Author by

William Jockusch

Free Graphing Calculator -- iOS: https://itunes.apple.com/us/app/free-graphing-calculator/id378009553?mt=8 Android: https://play.google.com/store/apps/details?id=com.jockusch.freegraphingcalculator Mac: https://itunes.apple.com/us/app/free-graphing-calculator-2/ Windows: https://www.microsoft.com/store/apps/9pgllk5gj04h

Updated on November 04, 2020

Comments

  • William Jockusch
    William Jockusch over 3 years

    What I want to do is set the initial zoom scale [and in some cases, content offset, but that is less important] for an external website. But after the app initially sets the zoom and offset, the user should be able to change them, and the app should not interfere.

    Some related information is available here, here, and here. But as far as I can tell, none of does what I want.

    The meta setting approach looks promising -- but how would I set it when I don't control the html content that will be loaded?