【问题标题】:Landscape Mode ONLY for iPhone or iPad横向模式仅适用于 iPhone 或 iPad
【发布时间】:2011-02-08 12:28:36
【问题描述】:

我想创建一个不使用纵向模式的应用程序。

我不确定是否需要编辑 plist 或除 plist 之外还有代码

【问题讨论】:

    标签: iphone objective-c cocoa-touch uikit ipad


    【解决方案1】:

    Code found here

    以横向模式启动

    iPhone OS 中的应用程序正常 以纵向模式启动以匹配 主屏幕的方向。如果你 有一个在两者中运行的应用程序 纵向和横向模式,您的 应用程序应始终启动 肖像模式最初,然后让 它的视图控制器旋转 接口根据需要根据 设备的方向。如果你的 应用程序以横向模式运行 但是,您必须执行 以下步骤使其在 最初是横向的。

    • 在应用程序的 Info.plist 文件中,添加 UIInterfaceOrientation
      键并将其值设置为
      横向模式。景观
      方向,您可以设置值
      这把钥匙到
      UIInterfaceOrientationLandscapeLeft

      UIInterfaceOrientationLandscapeRight.

    • 在横向模式下布置视图并确保它们的 设置了自动调整大小选项 正确。

    • 覆盖视图控制器的shouldAutorotateToInterfaceOrientation: 方法并仅对
      返回 YES 所需的横向方向和NO
      用于纵向。

    【讨论】:

    • 总是忘记 shouldAutorotateToInterfaceOrientation 步骤,否则你的整个 UI 会出现横向...
    【解决方案2】:

    要使您的应用横向模式,您应该使用“支持的界面方向”。 (Targets -> YourApp -> Supported Interface Orientations -> Landscape Left & Right)

    您还应该在应用程序的 Info.plist 文件 () 中设置应用程序的方向,方法是在 Supported interface orientations 键后面附加值 Landscape (left home button)Landscape (right home button)

    您可以使用willRotateToInterfaceOrientation 和/或didRotateFromInterfaceOrientation 来处理方向更改。


    shouldAutorotateToInterfaceOrientation 已从 iOS 6 及以后弃用。

    shouldAutorotateToInterfaceOrientation 返回UIDeviceOrientationLandscapeLeft/Right 应该使您的应用程序“风景”:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    

    还可能更改您应用的Info.plistView Orientation(如上所述)。


    此外,我建议在属性检查器中将视图的方向更改为Landscape

    【讨论】:

    • 这在当时并不存在,但很高兴了解更新的选项
    【解决方案3】:

    你也可以把它全部缩短为

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }
    

    【讨论】:

      【解决方案4】:

      编辑 plist 以仅支持横向,然后确保在每个 uiviewcontroller/uitabbar 等中,在 shouldAutoRotateToInterfaceOrientation 中,return 表示 return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));

      【讨论】:

      • 或者更好: if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) { return YES; } 返回否;
      • 甚至更短:return (UIInterfaceOrientationIsLandscape(interfaceOrientation))
      • 没有括号更短:-)
      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 2019-01-21
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多