UIWebView's zoom range is very limited

≡放荡痞女 提交于 2019-12-08 09:18:21

问题


I have a test UIWebView. I loaded a small amount of custom HTML into it. Nothing special -- two pages, a little text, and a small image on one of the pages. I am using it to test out the class.

One problem I am having is that my ability to zoom in and out is very limited. It seems that even if I set the minimumZoomScale and maximumZoomScale properties of the UIWebView's UIScrollView, something resets them to a factor of 1.5 or smaller ratio. And if I try to set them often, the app gets very unhappy and is prone to crash.

Can anyone explain what is going on?


回答1:


You need to put the following line in the head section of your HTML document.

<meta name="viewport" content="user-scalable=yes,maximum-scale=5.0,minimum-scale=0.25" />

Docs regarding this: http://developer.apple.com/library/safari/#documentation/appleapplications/reference/SafariHTMLRef/Articles/MetaTags.html




回答2:


I know about this JavaScript tag. But I didn't know how to implement this tag. After spend some time I got my solution.
Try below code. Change your minimum-scale and maximum-scale as per your requirement.

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


来源:https://stackoverflow.com/questions/7048804/uiwebviews-zoom-range-is-very-limited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!