设置 UIWebView 的最大缩放比例

3

我尝试通过以下方式为UIWebview设置最大缩放比例:

        webView.scrollView.maximumZoomScale=2.0;
        webView.scrollView.minimumZoomScale=1.0;

但是现在无法做到。如果有任何人有想法,请告诉我们。

  1. webView不能缩放?
  2. 最大缩放比例无法限制webView?
- TedCheng
1个回答

1

按照以下步骤:

1)添加静态const float zoomSc = 3.0; 同时在.h文件中添加UIScrollViewDelegate

2)yourWebView.scrollView.delegate = self;

3)添加下面的scrollView代理方法

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{

  if (scale > zoomSc)
  {
    scrollView.zoomScale= zoomSc;
  }
}

编辑:更改缩放因子范围

UIWebViewDelegate方法中添加以下内容。

注意:不要忘记为UIWebView设置委托。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   //restrict zoomscale by adding javascript code
   //make changes to only maximum-scale value as i have set it to 10.0.
   NSString *strJSFunction = [NSString stringWithFormat:@"function increaseMaxZoomFactor(){ var element = document.createElement('meta'); element.name = \"viewport\"; element.content = \"maximum-scale=10.0\"; var head = document.getElementsByTagName('head')[0]; head.appendChild(element); }"];

  [webView stringByEvaluatingJavaScriptFromString:strJSFunction];
  [webView stringByEvaluatingJavaScriptFromString:@"increaseMaxZoomFactor()"];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接