我编码一个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" />
没有什么改变,在我的代码中,我正在实现以下函数来处理rotate,但它没有提供任何东西
- (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)
解决方法
尝试在UIWebView上设置约束.当视图旋转时,空间约束应自动调整.
在故事板中,查找右下方的菜单图标.选择自动布局图标(看起来像领带战士),然后选择“添加缺少约束”或“重置为建议的约束”.