如何重置UIWebView的缩放?我已经在使用scalesPagesToFit = YES;

发布于 2024-09-10 19:24:53 字数 423 浏览 2 评论 0原文

过去一周我一直在寻找这个问题的答案。

我有一个 UIWebView,位于 UIScrollView 内部。一切都很好,但我希望 UIWebView 的内容在方向改变时重置其缩放。

在 UIWebView 内的 HTML 中,我将视口的宽度(带元标记)设置为“device-width”,然后在 Obj-C 端设置 scalesPagesToFit = YES;

我尝试过用 javascript 重置缩放;通过在运行时替换元标记;重新加载;访问 UIWebView 内部的 UIScrollView;等等...

但没有成功。

有大神知道解决办法吗?

我唯一能想到的就是每次改变方向时重新创建 UIWebViews,但这会使它们在渲染内容时闪烁为白色,这看起来很糟糕:(

有什么想法吗?

非常感谢, 安德烈

I've been looking for the past week for the answer to this question.

I have a UIWebView, inside of a UIScrollView. Everything works great, but I want the content of the UIWebView to reset its zoom, when the orientation changes.

In the HTML inside the UIWebView, I set the width of the viewport (w/ a meta tag) to "device-width" and then on the Obj-C side, I set the scalesPagesToFit = YES;

I've tried resetting the zoom with javascript; by replacing the meta tags in runtime; reloading; accessing the UIScrollView inside of the UIWebView; etc...

but with no success.

Any of you gods know a workaround?

The only one I can think off is to recreate the UIWebViews every time we change the orientation, but that makes them flash to white whilst rendering content, which looks terrible :(

Any thoughts?

Many thanks,
Andre

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

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

发布评论

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

评论(5

醉态萌生 2024-09-17 19:24:53

我只是在这里猜测并没有尝试过,但据我所知 UIWebView 有一个 UIScrollView 子项。所以人们应该能够做到:

for (UIScrollView *scroll in [myWebView subviews]) {
    // Make sure it really is a scroll view and reset the zoom scale.
    if ([scroll respondsToSelector:@selector(setZoomScale:)])
        [scroll setZoomScale:1.0];
}

I'm just guessing here and haven't tried, but AFAIK a UIWebView has a UIScrollView child. So one should be able to do:

for (UIScrollView *scroll in [myWebView subviews]) {
    // Make sure it really is a scroll view and reset the zoom scale.
    if ([scroll respondsToSelector:@selector(setZoomScale:)])
        [scroll setZoomScale:1.0];
}
逐鹿 2024-09-17 19:24:53

在 iOS 5+ 上,您可以访问scrollView。
只需执行:

[webView.scrollView setZoomScale:1.0];

On iOS 5+ you have access to scrollView.
Just do:

[webView.scrollView setZoomScale:1.0];

記柔刀 2024-09-17 19:24:53

如果您想以编程方式执行此操作,这是我能找到的唯一方法来完成它:(如果您愿意,请指定您自己的尺寸,我在表单字段中输入内容后尝试缩小)

UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];

If you want to do it programmatically this is the only way I could find to accomplish it: (specify your own sizes if you wish, i was attempting to zoom out after typing into a form field)

UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];
一抹淡然 2024-09-17 19:24:53

更新:
使用时缩小尺寸无法正常工作

[[[webView subviews] lastObject] setZoomScale:0.25];

页面上缩小尺寸的图像质量非常糟糕。正在做:

[[[webView subviews] lastObject] setZoomScale:0.25 animated:YES];

修复它。所以最后一行是您可以使用的。

webView 是 UIWebView 的子类,它位于某个 IB 文件上。我根本没有使用视口。我发现应该选择从 Cocoa Touch 一侧执行此操作或使用 JS。

我用过:

webView.scalesPageToFit = YES;

我想知道是否有办法重置scalesPageToFit。

Update:
Downscaling wasn't working properly when using

[[[webView subviews] lastObject] setZoomScale:0.25];

The quality of the images being downscaled on the page was awful. Doing:

[[[webView subviews] lastObject] setZoomScale:0.25 animated:YES];

Fixed it. So that last line is the one you could use.

webView was subclassed of a UIWebView which lies on some IB file. I didn't use the Viewport at all. I find that one should pick by either doing this from the Cocoa Touch side or use JS.

I used:

webView.scalesPageToFit = YES;

I wonder if there's a way of resetting the scalesPageToFit.

人间☆小暴躁 2024-09-17 19:24:53

根据 Captnwalker1 的答案,我想出了这个:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(toInterfaceOrientation ==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationMaskPortraitUpsideDown)
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
    else
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
}

所以加载你的 webview 图像,图像在旋转时将重置其大小。

Adapting from Captnwalker1's answer, I came up with this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(toInterfaceOrientation ==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationMaskPortraitUpsideDown)
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
    else
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
}

So load your webview image, and the image will reset it's size when rotated.

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