9

我正在编写一个 iPhone 应用程序,其中我有一个UIWebView加载具有响应式设计的网站。

我遇到的问题是当我将设备旋转到横向时:UIWebView旋转但它保持与纵向模式相同的宽度。

我花了最后 5 个小时在网上搜索,我找到了一些我使用但没有成功的解决方案......

在我正在使用的 html 页面中

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

我尝试使用

<meta name="viewport" content="width=device-width" />

没有任何改变,在我的代码中,我正在实现以下函数来处理旋转,但它什么也没提供

- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.myWebView stringByEvaluatingJavaScriptFromString:
    [NSString stringWithFormat:
    @"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
    (int)self.myWebView.frame.size.width]];

    NSLog(@"orientation width: %d", (int)self.myWebView.frame.size.width);

}

我还选中了“缩放页面以适应”复选框,

(使用 xcode 5,带有 iphone4s 和 ios7)

4

2 回答 2

28

尝试在 UIWebView 上设置约束。当视图旋转时,空间约束应该会自动调整。

在情节提要中,查找右下角的菜单图标。选择自动布局图标(看起来像平手),然后选择“添加缺失的约束”或“重置为建议的约束”。 在此处输入图像描述

于 2013-09-29T17:05:36.380 回答
11

最后,这解决了我的问题

- (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   [self.myWebView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
于 2013-09-28T14:55:32.917 回答