gpt4 book ai didi

iPhone UIWebView 宽度在缩放操作 + UIInterfaceOrientation 更改后不适合

转载 作者:行者123 更新时间:2023-12-03 18:24:33 25 4
gpt4 key购买 nike

我使用 UIWebView 创建了一个简单的 iPhone 应用程序(缩放页面以适合 = YES,shouldAutorotateToInterfaceOrientation = YES)并加载了一个网页,例如https://stackoverflow.com/

旋转设备显示 UIWebView 会自动调整大小以适应宽度。很好。

错误:放大页面并缩小。现在旋转设备会在其中一个方向上显示 UIWebView 的宽度很奇怪(如果你放大横向,则纵向宽度很奇怪,反之亦然)。仅当您导航到另一个页面时,此行为才会修复。

正确:在 Mobile Safari 中加载相同的 URL。无论缩放练习如何,旋转都有效且宽度适合。

这是一个 UIWebView 错误吗(可能不是)?或者是否需要做一些事情才能让事情像在 Mobile Safari 中一样“正常工作”?

最佳答案

我发现了一些对我有用的东西。问题是,当 uiwebview 更改其方向时,Web 内容会缩放以适应视口(viewport)。但是scrollview subview 的zoomscale参数没有正确更新(minimumZoomScale和maximumZoomScale也没有更新

然后我们需要在willRotateToInterfaceOrientation处手动完成:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
CGFloat ratioAspect = webview.bounds.size.width/webview.bounds.size.height;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortraitUpsideDown:
case UIInterfaceOrientationPortrait:
// Going to Portrait mode
for (UIScrollView *scroll in [webview subviews]) { //we get the scrollview
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:@selector(setZoomScale:)]){
scroll.minimumZoomScale = scroll.minimumZoomScale/ratioAspect;
scroll.maximumZoomScale = scroll.maximumZoomScale/ratioAspect;
[scroll setZoomScale:(scroll.zoomScale/ratioAspect) animated:YES];
}
}
break;
default:
// Going to Landscape mode
for (UIScrollView *scroll in [webview subviews]) { //we get the scrollview
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:@selector(setZoomScale:)]){
scroll.minimumZoomScale = scroll.minimumZoomScale *ratioAspect;
scroll.maximumZoomScale = scroll.maximumZoomScale *ratioAspect;
[scroll setZoomScale:(scroll.zoomScale*ratioAspect) animated:YES];
}
}
break;
}
}

希望这有帮助!

关于iPhone UIWebView 宽度在缩放操作 + UIInterfaceOrientation 更改后不适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2890673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com