问题描述
我正在编写一个 iPhone 应用程序,其中我有一个 UIWebView
可以加载具有响应式设计的网站.
I'm coding an iPhone app in which I have a UIWebView
that loads a website with a responsive design.
我遇到的问题是当我将设备旋转到横向时:UIWebView
旋转但它保持与纵向模式相同的宽度.
The problem I have is when I rotate the devices to landscape: the UIWebView
rotates but it keeps the same width as the portrait mode.
我花了最后 5 个小时在网上搜索,我找到了一些我使用但没有成功的解决方案......
I spent the last 5 hours searching all over the net, I found some solutions that I used but without success...
在我正在使用的 html 页面中
in my html page i'm using
<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" />
什么都没有改变,在我的代码中我正在实现以下函数来处理旋转,但它什么也没做
nothing changed, in my code I'm implementing the following function to handle the rotate, but it gives nothing
- (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);
}
我还选中了缩放页面以适应"复选框,
I also checked the "Scale Page to Fit" checkbox,
(使用 xcode 5,使用 iphone4s 和 ios7)
(using xcode 5, with an iphone4s and ios7)
推荐答案
尝试在 UIWebView 上设置约束.当 View 旋转时,空间约束应该会自动调整.
Try setting constraints on the UIWebView. When the View rotated, the space constraints should automatically adjust.
在故事板中,查找右下角的菜单图标.选择自动布局图标(看起来像平手),然后选择添加缺失的约束"或重置为建议的约束".
In the storyboard, look for the menu icons in the lower right. Select the auto layout icon (looks like a tie fighter), and then select either 'Add Missing Constraints' or 'Reset to Suggested Constraints'.
这篇关于将方向更改为横向后,uiwebview 未调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!