【问题标题】:UIWebView: Forcing to landscape (locking) and aspectfit to windowUIWebView:强制横向(锁定)和aspectfit到窗口
【发布时间】:2014-08-04 10:09:14
【问题描述】:

所有,我在 ViewController 中有一个 UIWebView。控制器已正确连接并与前一个 tableview 正确连接。但是,我正在努力完成两件事:1.强制 UiWebView(嗯,整个场景)横向(我正在显示来自 url 的地图)并且不允许用户旋转,以及 2.让 webview 在约束范围内显示设备。现在,当 UIWebView 显示为横向时,它会跑出屏幕。

我已尝试重置约束、更新缺少的约束、情节提要中的方向锁定、情节提要中的视图>模式>aspectfit 并在视图控制器方法文件中强制其横向(设置为 aspectfit 而不是 aspectfill)。

我在这里阅读了之前的问题,这些问题说在以前的 iOS 版本中不可能只将一个场景锁定为纵向/横向。我发现这可能会随着 iOS7 而改变。

我正在寻求有关如何锁定视图和缩小 uiwebview 中显示的网页以适合 iphone 应用程序尺寸的指导。谢谢

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller uiwebview


    【解决方案1】:

    四个步骤:

    1. 子类 UINavigationController
    2. 覆盖 UINavigationController 的 preferredInterfaceOrientationForPresentation: 方法

      (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
      
      UIDevice *myDevice = [UIDevice currentDevice];
      UIDeviceOrientation deviceOrientation = myDevice.orientation;
      
      if (deviceOrientation == UIDeviceOrientationLandscapeLeft)
      {
          return UIInterfaceOrientationLandscapeRight;
      }
      else if (deviceOrientation == UIDeviceOrientationLandscapeRight)
      {
          return UIInterfaceOrientationLandscapeLeft;
      }
      else {
          return UIInterfaceOrientationLandscapeLeft;
      }
      

      }

    3. 将具有 UIWebView 的 UIViewController 设置为子类 UINavigationController 的 rootViewController

    4. 展示您的导航控制器

    如果上述方法不起作用,请尝试将以下内容应用于您的 tableViewController(即,将呈现您的 UINavigationController 子类的 viewController。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (BOOL)shouldAutorotate
    {
        return NO;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    

    您可能还需要在目标 > 常规下添加这些设置。

    这似乎是我在我的应用程序中强制横向运行所做的一切。

    【讨论】:

    • 应用程序崩溃了,尽管我知道这是我做错了。我已经继承了我原来的导航控制器。我在新的导航控制器中实现了提供的代码。然后我将 webviewcontroller 设置为新导航控制器的根视图控制器。这样做打破了我的 tableviewcontroller 的旧 segue,所以我将 segue 重置为从 tableviewcontroller>newnavcontroller。我认为我的错误属于segue的某个地方......
    • 现在实际上没有错误,但是方向没有变化。我已按照您的步骤进行操作,一切都井井有条(构建没有错误)。从 tableviewcontroller 转到 newnavcontroller,其中包含提供的代码。 Webviewcontroller 设置为此视图控制器的根。切换到 webviewcontroller 后,场景仍以纵向显示。会不会有什么东西可以在其他地方覆盖它?
    • 我有一些参考代码,直到明天才能查看。到时候我会告诉你的。
    • 我又回到了同样的问题上,搁置了几天。你碰巧有这个强制横向的简单 xcodeproj 源代码吗?我什至尝试使用您的方法创建一个空白应用程序,但无济于事。
    【解决方案2】:

    如果您希望将整个应用程序锁定为横向,您可以进入 info.plist 文件并从“支持的界面方向”列表中删除纵向条目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多