UIWebView无法自适应屏幕大小

3

我有一个应用程序,使用UIViewController在UIWebView中显示文章,其中一些文章有图片。 我已经设置好了当单击图片时调用

- (void)displayImage:(NSInteger)i {
ImageViewController * imageVC = [[ImageViewController alloc] initWithImageId:i];
[self.navigationController pushViewController:imageVC animated:YES];
[imageVC release];

这会加载另一个UIViewController。 我的所有视图都能很好地响应旋转。ImageViewController保持图片的比例,而ArticleViewController则用文字填充屏幕。但是,每当我在查看ImageViewController时将屏幕旋转到横向模式并按返回按钮时,上一个UIViewController会自动错误地调整大小,增加文本字体大小而不是在一行上添加更多单词。

编辑:

这是一篇文章视图控制器:

This is an article view controller

这是点击图像后出现的ImageViewController:

ImageViewCOntroller - portrait

现在如果我们在这里改变方向...

ImageViewCOntroller - landscape

然后点击返回按钮,文章内容被不适当地调整大小以适应窗口。

ArticleViewCOntroller - landscape bad

这可以通过将iPhone翻转两次来纠正。这是文章在横向模式下应该看起来的样子。

ArticleViewCOntroller - landscape good

问题与UIWebView在不可见时未能自适应大小有关。我该怎么做才能解决这个问题?


你找到解决办法了吗? - Fahim Parkar
3个回答

5

您需要在Interface Builder中进行此操作。

点击您的UIWebView并按Apple-3键以拉出“大小检查器”。

在“自动调整大小”下,确保框内的两个箭头被选中。这将确保视图大小始终最大化到其容器中。


1
我已经做了这个,它运行良好,除了我在原始问题中解释的情况。还有其他建议吗? - Tozar

1

您可以通过在服务器端添加不同的CSS样式来根据设备方向进行更改,例如:

/* Landscape */
@media screen and (min-width: 321px)
{   
   body{
     width: 320px;
   }    
}
/* Landscape */
@media screen and (min-width: 321px)
{ 
   body{
     width: 480px;
   }    
}

或者你可以从客户端更改视口: 如果你的页面有视口元标记:

<meta name="viewport" id="view" content="width=320px, minimum-scale=1.0, maximum-scale=1.0">

然后在您的代码中,每次方向更改时都可以更改此视口:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIDeviceOrientationLandscapeLeft)      
 [uiwebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"view\").setAttribute('content', 'width=480');"]];
}
....

并在方向为纵向时再次更改它


你的解决方案中的视口部分非常好用。谢谢! - Mihai Timar

1

供日后参考。您可以使用以下代码进行编程。

[webView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];


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