UIWebView - 加载外部网站,以编程方式设置初始缩放比例,并允许用户随后进行缩放 [英] UIWebView -- load external website, programmatically set initial zoom scale, and allow user to zoom afterwards
问题描述
是否有可能做到以上所有? SO给了我一个设置初始缩放比例的好方法此处。也就是说,要在我的webViewDidFinishLoad方法中包含以下行:
Is it possible to do all of the above? SO has given me a great way to set the initial zoom scale here. Namely, to include the following line in my webViewDidFinishLoad method:
[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.zoom = 5.0;"];
但我仍然不能做的是允许用户在程序初始后更改缩放比例设定它。如果我将scalePagesToFit设置为NO,则用户无法更改缩放比例。如果我将scalePagesToFit设置为YES,它将覆盖我的程序化缩放。
But what I still can't do is allow the user to change the zoom scale after the program initially sets it. If I set scalePagesToFit to NO, the user can't change the zoom scale. And if I set scalePagesToFit to YES, it overrides my programmatic zoom.
任何人都可以帮忙吗?
编辑:谢谢你的回应。但是,如果我缩放scrollView,事情会变得模糊不清。
thanks for the response. But if I zoom the scrollView, things blur a little.
推荐答案
您可以添加或修改< meta name ='viewport'
标签来实现这一点。有关meta / viewport标记的Apple文档:
You can add or modify a <meta name='viewport'
tag to achieve this. Apple documentation on the meta/viewport tag here:
以下是加载文档后添加标记的示例:
Here's an example of adding the tag once the document is loaded:
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
NSString* js =
@"var meta = document.createElement('meta'); " \
"meta.setAttribute( 'name', 'viewport' ); " \
"meta.setAttribute( 'content', 'width = device-width, initial-scale = 5.0, user-scalable = yes' ); " \
"document.getElementsByTagName('head')[0].appendChild(meta)";
[webView stringByEvaluatingJavaScriptFromString: js];
}
如果您要加载的网页提供了现有的元标记,则可能需要修改它而不是尝试向DOM添加新的元元素。这个SO问题讨论了这个技术:
If the web page you're loading provides an existing meta tag you may need to modify it rather than attempting to add a new meta element to the DOM. This SO question discusses the technique:
In我的测试应用程序我将 UIWebView
scalesPageToFit
设置为YES。
In my test app I set the UIWebView
scalesPageToFit
to YES.
这篇关于UIWebView - 加载外部网站,以编程方式设置初始缩放比例,并允许用户随后进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!