【问题标题】:uiwebview not resizing after changing orientation to landscape将方向更改为横向后,uiwebview 未调整大小
【发布时间】:2013-10-04 08:59:36
【问题描述】:

我正在编写一个 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)

【问题讨论】:

    标签: objective-c uiwebview xcode5 orientation landscape


    【解决方案1】:

    终于解决了我的问题

    - (void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
       [self.myWebView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    }
    

    【讨论】:

    • 如果您以编程方式执行此操作,这是正确答案。
    【解决方案2】:

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

    在故事板中,查找右下角的菜单图标。选择自动布局图标(看起来像一个平局),然后选择“添加缺失的约束”或“重置为建议的约束”。

    【讨论】:

    • 在 IOS 设备上运行良好(“添加缺少的约束”)。模拟器也会旋转,但似乎有点困惑(只有四分之三的旋转工作)。对我来说,不需要使用 Objective C。
    • 我不知道为什么,但在常规部署信息页面或项目 plist 中指定“颠倒”似乎不起作用。我总是将此方法添加到我的主视图控制器中以解决该问题,并确保支持所有其他方向:- (NSUInteger)supportedInterfaceOrientations { return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); }
    • 由于 iPhone 不允许用户在横向模式下锁定方向,因此纵向倒置模式提供该功能。但是,在 iPad 上,您可能会发现纵向倒置模式实际上可以按预期工作。除非您有特定的用例来证明不同的理由,否则建议简单地坚持这些默认值。
    • +1 供 tie-fighter 参考,您的解决方案也有效,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多