【问题标题】:How can I disable landscape orientation?如何禁用横向?
【发布时间】:2012-07-31 21:39:49
【问题描述】:

我正在制作一个 iPhone 应用程序,我需要它处于纵向模式,因此如果用户将设备侧向移动,它不会自动旋转。我该怎么做?

【问题讨论】:

    标签: iphone objective-c ios xcode orientation


    【解决方案1】:

    要禁用特定视图控制器的方向,您现在应该覆盖supportedInterfaceOrientationspreferredInterfaceOrientationForPresentation

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        // Return a bitmask of supported orientations. If you need more,
        // use bitwise or (see the commented return).
        return UIInterfaceOrientationMaskPortrait;
        // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        // Return the orientation you'd prefer - this is what it launches to. The
        // user can still rotate. You don't have to implement this method, in which
        // case it launches in the current orientation
        return UIInterfaceOrientationPortrait;
    }
    

    如果您的目标是 iOS 6 之前的版本,您需要 shouldAutorotateToInterfaceOrientation: 方法。通过更改它何时返回是,您将确定它是否会旋转到所述方向。这将只允许正常的纵向。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait); 
        
        // Use this to allow upside down as well
        //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }
    

    请注意,shouldAutorotateToInterfaceOrientation: 在 iOS 6.0 中已被弃用

    【讨论】:

    • 如果视图控制器在导航控制器中,那么导航控制器就是需要实现这些的地方。
    • @MrRogers 我这样做了,但它不起作用。请参阅pastebin.com/dLfLLP2j 每个控制器都正确实现了supportedInterfaceOrientations。即使从 iPhone Plus 的主屏幕中的设备启动应用程序,第一个视图控制器也能正确显示纵向。但是当我导航到另一个控制器时,来回旋转设备以将另一个控制器设置为横向,然后返回,第一个控制器现在处于横向状态。我想要的是第一个控制器是纵向的,第二个是 .all。
    • 我建议将 NSUintegersupportedInterfaceOrientations(第一行代码)替换为 UIInterfaceOrientationMask。 Xcode 8 警告“在 'supportedInterfaceOrientations' 的实现中返回类型冲突:'UIInterfaceOrientationMask' (aka 'enum UIInterfaceOrientationMask') vs 'NSUInteger' (aka 'unsigned long')”这很有意义。谢谢你的回答?
    【解决方案2】:

    Xcode 5 及以上版本

    • 在左侧边栏中的项目导航器中单击您的项目以打开项目设置
    • 转到常规标签。
    • 设备方向下的部署信息部分取消选中您不需要的选项

    【讨论】:

    • 我无法取消选择我的风景。它会立即将它们重新打开。此外,如果我从 Info.plist 中删除它们,它会将它们放回去。
    • @skywinder 主界面不存在怎么办?
    【解决方案3】:

    Xcode 4 及以下

    对于那些错过它的人:您可以使用项目设置屏幕来修复整个应用程序的方向(无需覆盖每个控制器中的方法):

    就像切换支持的界面方向一样简单。您可以通过单击左侧面板中的项目 > 应用程序目标 > 摘要选项卡来找到。

    【讨论】:

    • 这个已经过时了;用户界面现在不同了。
    【解决方案4】:

    针对 iPhone 和 iPad(通用)的最简单解决方案 - 它删除 info.plist 文件或 Project -> Info -> Custom iOS Target Properties 中不必要的方向.

    只需从列表中添加或删除方向项:

    • iPhone支持的界面方向
    • iPad 支持的界面方向 (iPad)

    【讨论】:

    • 我刚试过这个,但我不得不将“支持的界面方向”替换为“支持的界面方向(iPhone)”,否则它会覆盖 iPad 设置。
    【解决方案5】:

    如果您想禁用 iPhone 和 iPad 的横向显示。

    转到目标并转到常规标签。查看下面的屏幕并取消选择横向左侧和横向右侧

    在这种情况下,只有 iPhone 横向模式将被禁用,而不是 iPad。 对于 iPad,所有模式均已启用。如果您想从 Universal 到 iPad 选择设备选项。它看起来像这样。请参阅下面的屏幕。

    现在您需要为 iPad 取消选择所有模式除了肖像。请看下面的截图。

    现在您已成功为所有设备禁用了所有模式除了纵向

    【讨论】:

      【解决方案6】:

      斯威夫特 3 如果你有一个 navigationController,像这样子类化它(仅用于纵向):

      class CustomNavigationViewController: UINavigationController {
      
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return UIInterfaceOrientationMask.portrait
        }
      
        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
          return UIInterfaceOrientation.portrait
        }
      }
      

      【讨论】:

        【解决方案7】:

        从您的类中完全删除方法 shouldAutorotateToInterfaceOrientation 也可以。如果你不打算轮换,那么在你的类中使用该方法是没有意义的,代码越少越好,保持干净。

        【讨论】:

        • 这个答案现在已经过时了; shouldAutorotateToInterfaceOrientation 已被弃用多年。
        • @VaddadiKartick 这也是与此答案中提到的方法不同的方法;我不确定你的意思是什么。
        • 哦,对不起。我错过了。
        【解决方案8】:

        Xcode 8、Xcode 9、Xcode 10 及以上版本

        【讨论】:

        • 知道为什么这不起作用。我在 Xcode 设置/Info.plist 中选择了此按钮,然后为 iphone 取消选择它们,检查 Info.plist 是否只有一个用于纵向模式的条目。但在 iPhone 应用程序静止图像上可以在每个屏幕上旋转
        【解决方案9】:

        如果您希望应用程序仅在纵向模式下运行,

        在您的 viewDidLoad 方法下方复制以下代码行

        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
           get {
              return .portrait
           }
        }
        

        这将为您的应用程序锁定横向模式。同样,将 .portrait 替换为 .landscape 将在横向模式下运行您的应用程序。

        如果未指定,您的应用程序将在两种模式下运行。

        或者,您可以从 Xcode Builder 锁定方向,如下所示。

        【讨论】:

          猜你喜欢
          • 2021-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-24
          • 1970-01-01
          • 1970-01-01
          • 2016-02-08
          • 2013-08-13
          相关资源
          最近更新 更多