ios UIWebView 在加载外部网站时以编程方式设置初始缩放比例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8028051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:15:13  来源:igfitidea点击:

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

iphoneiosobjective-cuiwebviewzoom

提问by William Jockusch

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?

元设置方法看起来很有希望——但是当我不控制将加载的 html 内容时,我将如何设置它?

回答by Srikar Appalaraju

Try this for setting the initial zoom level -

试试这个来设置初始缩放级别 -

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

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

也不要忘记设置scalesPageToFit为NO,你就完成了。

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.

如果您将此设置为 YES,则网页会缩放以适应并且用户可以放大和缩小。如果否,则禁用用户缩放。默认值为否。

回答by Heo ??t Hades

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:

如果您想为您的 web 视图设置初始缩放,然后您的 web 视图可以缩放。您可以尝试在 webViewDidFinishLoad 协议方法中将元标记添加到 HTML 字符串的 head 标记中。您可以更改 'initial-scale'、'minimum-scale' 和 'maximum-scale' 以适应您的需要。看起来像这样:

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

回答by sazz

Why not:

为什么不:

   [self.webView.scrollView setZoomScale:2.0 animated:YES];

回答by Rajal

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

最好的一个发现尝试在您的 html 页面中添加元标记,在您的标记中。它会在眨眼间修复。尝试使用苹果链接并为您选择合适的元标记。

回答by linta

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

如果您在 uiwebview 中加载 html 和 javascript,则在视口中设置 user-scalable=1.0。