UIWebView——加载外部网站,以编程方式设置初始缩放比例,并允许用户随后缩放
是否可以完成以上所有操作? SO给了我一个很好的方法来设置初始缩放比例这里。也就是说,在我的 webViewDidFinishLoad 方法中包含以下行:
[webView stringByEvaluatingJavaScriptFromString: @"document.body.style.zoom = 5.0;"];
但我仍然不能做的是允许用户在程序最初设置缩放比例后更改缩放比例。如果我将scalePagesToFit 设置为NO,则用户无法更改缩放比例。如果我将scalePagesToFit设置为YES,它会覆盖我的编程缩放。
有人可以帮忙吗?
编辑:感谢您的回复。但是如果我缩放滚动视图,事情就会变得有点模糊。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以添加或修改
标记来实现此目的。有关元/视口标签的 Apple 文档位于此处:
https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
这是添加文档加载后添加标记:
如果您正在加载的网页提供了现有的元标记,您可能需要修改它,而不是尝试向 DOM 添加新的元元素。这个SO问题讨论了该技术:
我可以动态更改移动 safari 中的视口元标记吗?
在我的测试应用程序中,我将
UIWebView
scalesPageToFit
设置为 YES。You can add or modify a
<meta name='viewport'
tag to achieve this. Apple documentation on the meta/viewport tag here:https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
Here's an example of adding the tag once the document is loaded:
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:
Can I change the viewport meta tag in mobile safari on the fly?
In my test app I set the
UIWebView
scalesPageToFit
to YES.看起来底层的
UIScrollView
从 iOS 5.x 开始就可以访问。在
webViewDidFinishLoad:
中,您可以请求缩放,例如:即使将
scalesPageToFit
设置为YES
,这似乎也能工作。It looks like the underlying
UIScrollView
is accessible starting from iOS 5.x.In
webViewDidFinishLoad:
you can request a zoom, for example:This seems to work even with
scalesPageToFit
set toYES
.这实际上很棒。我需要具有缩放功能,而scalePagesToFit 是“否”。虽然你的代码 TomSwift 没有按照我的需要工作,但对其进行轻微调整就可以了。
我在 iOS 6.x 上运行这个...效果非常好。
很棒的提示汤姆。
This is actually brilliant. I needed to have zoom capability while scalePagesToFit is NO. Although your code TomSwift did not work as I needed, a slight adjustment to it did the trick.
I am running this on iOS 6.x...works like a charm.
Awesome tip Tom.
尝试更改设置这些命令的顺序 - 首先将scalePagesToFit设置为YES,然后:
Try changing the order that you are setting these commands - first scalePagesToFit to YES and then:
与我的答案的不同之处在于您在设置宽度之前清除视口。
这允许 Web 视图旋转或调整大小(加载后),而无需重置属性更改。
The difference with my answer is you are clearing the viewport before setting the width.
This allows for the web view to be rotated or resized (after being loaded) without resetting your attribute changes.