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

5

7

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?

Pharyngitis answered 6/11, 2011 at 15:25 Comment(0)
Z
32

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.

Zelig answered 6/11, 2011 at 15:29 Comment(2)
I put this into webViewDidFinishLoad, and it works great, thanks! But I do want to allow the user to change my initial zoom. I do NOT want the page to be scaled to fit [which it isn't] -- but user zooming is disable.Pharyngitis
Thanks a lot , This is the easiest solution i found.. I wish i could give 100 vote ups. ;)Candlemas
T
5

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];
}
Trilingual answered 4/11, 2016 at 6:49 Comment(1)
Thanks I know about this tags but I don't know how to implement. You save my day.Bowlds
B
2

Why not:

   [self.webView.scrollView setZoomScale:2.0 animated:YES];
Blither answered 15/12, 2014 at 13:44 Comment(0)
P
0

best one found try adding meta tag in your html page,in your tag.It gets repaired in a eye blink. Try the apple link and select the suitable meta tag for you.

Pratfall answered 12/5, 2015 at 10:24 Comment(0)
W
0

if you are loading html and javascriptin uiwebview then in viewport set user-scalable=1.0.

Wakerife answered 10/8, 2015 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.