即使将scalesPageToFit设置为YES后,UIWebview也不会缩放

发布于 2024-08-23 09:02:54 字数 621 浏览 10 评论 0原文

当用户选择 UISegmentedControl 上的选项卡时,我有一个 UIWebview 作为子视图加载。由于某种原因,我无法让它允许捏合/缩放。 我在 viewDidLoad: 方法中设置了以下代码,因此它应该可以工作。

self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: myWebView];

我尝试从 NIB 加载 UIWebView 并以编程方式创建它,但没有成功。我有什么遗漏的吗?是什么导致 webview 忽略捏合和缩放?

谢谢!

I have a UIWebview that is loaded as a subview when a user selects a tab on a UISegmentedControl. For some reason, I can not get it to allow pinch/zooming.
I set the following code in the viewDidLoad: method, so it should work.

self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: myWebView];

I tried loading a UIWebView from a NIB and creating it programmatically with no avail. Is there something I'm missing? What could be causing the webview to ignore pinching and zooming?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉栀 2024-08-30 09:02:55

根据我的经验,您需要在 UIWebView 加载之前设置scalesPageToFit。这意味着在 viewDidLoad 等之前进行设置。我所做的是将其设置在“shouldStartLoadWithRequest”中。

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //make sure that the page scales when it is loaded :-)
    theWebView.scalesPageToFit = YES;
    return YES;
}

我对文档的解释是,scalesPageToFit 属性指示页面将如何加载。它不会在事后改变事情。

希望这有帮助。

In my experience, you need to set scalesPageToFit before the UIWebView loads. This means setting before viewDidLoad etc. What I do is set it in "shouldStartLoadWithRequest"

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //make sure that the page scales when it is loaded :-)
    theWebView.scalesPageToFit = YES;
    return YES;
}

My interpretation of the documentation is that the scalesPageToFit property dictates how the page WILL be loaded. It does not alter things after the fact.

Hope this helps.

蓝梦月影 2024-08-30 09:02:55

仅供参考:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

可以工作,但

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES"/>

在 iPhone 上不起作用。文档说使用是/否。我想在这种情况下情况很重要。在 obj-c 中,值为 YES/NO 和 1/0

FYI:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

works but

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES"/>

does not on the iPhone. The documentation says to use yes/no. I guess case matters in this case. In obj-c the values are YES/NO and 1/0

留蓝 2024-08-30 09:02:55

您必须启用多点触控。捏合涉及屏幕上的多个手指:

[myWebView setMultipleTouchEnabled:YES]

You have to enable multi-touch. Pinch involves more than one finger on the screen:

[myWebView setMultipleTouchEnabled:YES]

情愿 2024-08-30 09:02:55

这是 Apple 在 webview 类参考中建议的内容。

重要提示:您不应在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆并错误处理。

This is what Apple suggests in the webview class reference.

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

好多鱼好多余 2024-08-30 09:02:54
  [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"];

似乎是合适的解决方案

  [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"];

seem to be the suitable solution

甜中书 2024-08-30 09:02:54

我通过设置视口解决了这个问题:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

祝你好运

I solved this with setting a view port:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

Good luck

末蓝 2024-08-30 09:02:54

我看到您正在设置 autoresizingMask。这是否意味着您创建了初始大小为 CGRectZero 的 UIWebView ?您可以与文档进行交互吗?我的意思是,滚动/点击有效吗?

I see you are setting the autoresizingMask. Does that mean you have created the UIWebView with an initial size of CGRectZero ? Can you interact with the document at all? I mean, does scrolling/tapping work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文