ios 如何在 UIScrollView 内的 UIWebView 上启用放大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2219685/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:53:00  来源:igfitidea点击:

How can I enable zoom in on UIWebView which inside the UIScrollView?

objective-cioscocoa-touchuiwebviewzoom

提问by calendarw

I have a UIWebViewwhich inside a UIScrollView(scrollview contain another component)

我在UIWebView里面有一个UIScrollView(滚动视图包含另一个组件)

I tried to enable multitouch both on Interface Builderor Programmaticon UIWebViewbut it still cannot zoom for html, do I need to handle both zoom in at the UIScrollViewand UIWebView? Or anything I haven't to set?

我试图让多点触控无论在界面生成器编程UIWebView,但它仍然不能放大为HTML,我需要在在同时处理变焦UIScrollViewUIWebView?还是我没有设置的东西?

回答by john geshrick

You MUST set scalesPageToFit=YES for any pinching and zooming to work on a UIWebView

您必须为任何捏合和缩放设置 scalesPageToFit=YES 才能在 UIWebView 上工作

回答by Jay Imerman

OK, you need to do both the above, but also the following. I had a web view in the main view, and that didn't work.

好的,您需要执行上述两项操作,还需要执行以下操作。我在主视图中有一个 web 视图,但没有用。

  1. As above, you first have to put a UIScrollView in the main view, then put the web view in the scroll view.
  2. As above, implement <UIScrollViewDelegate>in your view controller, drag the scroll view delegate to the view controller in Interface Builder, and implement the viewForZoomingInScrollViewmethod. This must return the pointer to the UIScrollView (return myScrollView).
  3. I created IBOutlet properties for both the web view and the scroll view - link them in the NIB to your view controller.
  4. On the Scroll View, go to the Attributes Inspector, set your Max and Min zoom factors (I set 0.5 to 5.0, that works well).
  5. On the Web View, in the Attributes Inspector:
  6. In the Web View section, select Scales Pages To Fit
  7. In the View section, select for Mode, "Top Left"
  8. In the View section at the bottom, check off User Interaction Enabled, and Multiple Touch Enabled
  1. 如上,你首先要在主视图中放置一个 UIScrollView,然后在滚动视图中放置 web 视图。
  2. 如上,<UIScrollViewDelegate>在你的视图控制器中实现,将滚动视图委托拖到Interface Builder中的视图控制器,并实现该viewForZoomingInScrollView方法。这必须返回指向 UIScrollView 的指针(返回 myScrollView)。
  3. 我为 web 视图和滚动视图创建了 IBOutlet 属性 - 在 NIB 中将它们链接到您的视图控制器。
  4. 在滚动视图上,转到属性检查器,设置最大和最小缩放系数(我设置为 0.5 到 5.0,效果很好)。
  5. 在 Web 视图中,在属性检查器中:
  6. 在“Web 视图”部分中,选择“缩放页面以适合”
  7. 在视图部分,选择模式,“左上角”
  8. 在底部的“视图”部分,勾选“启用用户交互”和“启用多点触控”

回答by robocat

With JavaScript you can control the zoom level, although the oly solution I have found doesn't look smooth.

使用 JavaScript 您可以控制缩放级别,尽管我发现的 oly 解决方案看起来并不流畅。

Say you have in <head>:

假设你有<head>

<meta id="vp" name="viewport" content="width=768,initial-scale=1.0">

To zoom to 4x, and still allow the user to change zoom, change the content twice:

要缩放到 4 倍,并且仍然允许用户更改缩放,请更改内容两次:

var vp = document.getElementById('vp');
vp.content = "width=767,minimum-scale=4.0,maximum-scale=4.0,user-scalable=yes";
vp.content = "width=768,minimum-scale=0.25,maximum-scale=10.0,user-scalable=yes";

Toggling the width is very important - otherwise Mobile Safari has serious repainting bugs (due to over-optimisation).

切换宽度非常重要 - 否则 Mobile Safari 会出现严重的重绘错误(由于过度优化)。

You cannot just set initial-scaleagain - it is ignored the second time.

您不能initial-scale再次设置- 第二次会忽略它。

回答by apenwarr

You need to implement the viewForZoomingInScrollView method in your controller, or zooming won't do anything. (I don't really know why this should be needed, but there you go.)

您需要在控制器中实现 viewForZoomingInScrollView 方法,否则缩放将无济于事。(我真的不知道为什么需要这样做,但是你去了。)

For detailed information, see http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html.

有关详细信息,请参阅http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html