xcode 如何允许缩放 UIWebView(尝试了一切)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7255621/
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
How to allow zooming of UIWebView (tried everything)
提问by pixelbitlabs
I have tried literally every bit of code I have found to try and make this one page zoom in and out but no matter what, the text still overlaps the screen and the page in the UIWebView will simply not fit to the screen.
我已经尝试了几乎所有我发现的代码来尝试放大和缩小这个页面,但无论如何,文本仍然与屏幕重叠,并且 UIWebView 中的页面根本不适合屏幕。
I've tried instructions here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/9112-uiwebview-zoom-pinch.html
我在这里尝试过说明:http: //www.iphonedevsdk.com/forum/iphone-sdk-development/9112-uiwebview-zoom-pinch.html
I've tried adding: webView.scalesPageToFit = TRUE;
我试过添加: webView.scalesPageToFit = TRUE;
I've set it to UserInteractionEnabled
.
我已经设置为UserInteractionEnabled
.
But nothing seems to work at all.
但似乎没有任何效果。
Is this to do with the coding of the webpage or is it to do with the UIWebView?
这是与网页的编码有关还是与UIWebView有关?
Thank you,
谢谢,
James
詹姆士
回答by Cullen SUN
First of all. Refer to UIWebView Class reference, you need to set scalesPageToFit.
Apple says: scalesPageToFit If 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.
If you view the source of the page, you should be able to find //meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"//.
In order to show you the Zoom effect. I want to replace it with: //meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=1;"//.
Run the following javascript for UIWebview's method stringByEvaluatingJavaScriptFromString: in - (void)webViewDidFinishLoad:(UIWebView *)webView{ }
function setScale(){ var all_metas=document.getElementsByTagName('meta'); if (all_metas){ var k; for (k=0; k<all_metas.length;k++){ var meta_tag=all_metas[k]; var viewport= meta_tag.getAttribute('name'); if (viewport&& viewport=='viewport'){ meta_tag.setAttribute('content',"width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=1;"); } } } }
首先。参考 UIWebView 类参考,需要设置 scalesPageToFit。
Apple 说: scalesPageToFit 如果为 YES,则网页被缩放以适合并且用户可以放大和缩小。如果否,则禁用用户缩放。默认值为否。
如果查看页面源码,应该可以找到//meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;"//。
为了向您展示缩放效果。我想将其替换为://meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=1;"//。
为 UIWebview 的方法 stringByEvaluatingJavaScriptFromString 运行以下 javascript: in - (void)webViewDidFinishLoad:(UIWebView *)webView{ }
function setScale(){ var all_metas=document.getElementsByTagName('meta'); if (all_metas){ var k; for (k=0; k<all_metas.length;k++){ var meta_tag=all_metas[k]; var viewport= meta_tag.getAttribute('name'); if (viewport&& viewport=='viewport'){ meta_tag.setAttribute('content',"width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable=1;"); } } } }
回答by Luke
I looked at the page source for the link you provided in the comments, and found this:
我查看了您在评论中提供的链接的页面源代码,发现了以下内容:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
This is what is "locking" the viewport down and not allowing for zooming.
这就是“锁定”视口并不允许缩放的原因。
You'll find some good information and the tags better explained here:
您会在这里找到一些很好的信息和更好的标签解释:
回答by Cullen SUN
some webpages are mobile site, fixed size. e.g. you use iPhone safari to open Google's home page
一些网页是移动网站,固定大小。例如,您使用 iPhone safari 打开 Google 的主页
回答by Nekto
I've tried your link (dhsb.org/index.phtml?d=190350) to open in Safarion my i4 and it works, as you've described (text overlaps the screen and zooming is off). So the problem is in the website, not in your code.
我已经试过你的链接 (dhsb.org/index.phtml?d=190350)在我的 i4 上的Safari 中打开,它可以工作,正如你所描述的(文本与屏幕重叠并且缩放关闭)。所以问题出在网站上,而不是在你的代码中。