UIWebView -- 加载外部网站,以编程方式设置初始缩放比例,然后允许用户缩放 [英] UIWebView -- load external website, programmatically set initial zoom scale, and allow user to zoom afterwards

查看:20
本文介绍了UIWebView -- 加载外部网站,以编程方式设置初始缩放比例,然后允许用户缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以上都可以吗?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.

谁能帮忙?

感谢您的回复.但是如果我缩放滚动视图,事情会变得有点模糊.

thanks for the response. But if I zoom the scrollView, things blur a little.

推荐答案

您可以添加或修改 标签来实现此目的.关于 meta/viewport 标签的 Apple 文档:

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:

- (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:

我可以更改吗移动 safari 中的视口元标记?

在我的测试应用中,我将 UIWebView scalesPageToFit 设置为 YES.

In my test app I set the UIWebView scalesPageToFit to YES.

这篇关于UIWebView -- 加载外部网站,以编程方式设置初始缩放比例,然后允许用户缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆