【问题标题】:Different Device Orientation according to device (iPhone or iPad)根据设备(iPhone 或 iPad)的不同设备方向
【发布时间】:2015-04-16 02:42:35
【问题描述】:

我正在开发一个具有以下要求的通用项目:

  • 对于 iPhone,我只想要纵向。
  • 对于 iPad,只有横向。

对于 iOS 8 (Swift),我该怎么做?

【问题讨论】:

  • 你试过Xcode中的info.plist吗?
  • 嗨@ScarletMerlin。感谢您的意见。我不知道我是怎么错过“支持的界面方向(iPad)”的关键。使用此键,它根据要求工作。随意创建一个答案,以便我投票。干杯,伙计。
  • 别担心,我不需要批准答案中的分数。 info.plist 是您告诉 iphone 处理全局设置的方式。您也可以在代码上执行此操作,但几乎不推荐。

标签: ios screen-orientation


【解决方案1】:

按照@ScarletMerlin 的建议,在我看来,更改 info.plist 中的键是我必须满足的要求的最佳解决方案(每种设备的固定方向类型)。

这是我使用的设置的打印屏幕。也许它可以帮助其他有类似疑问的开发人员。

相关源码如下:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

【讨论】:

  • 这是否会覆盖在 Target > General > Deployment Info > Device Orientation 下检查的内容?
【解决方案2】:

我的建议是检查您的应用程序正在运行的硬件。为此,请使用这行代码。

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

然后,一旦您检测到您的硬件,使用 shouldAutorotateToInterfaceOrientation 阻止方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    return UIInterfaceOrientationIsLandscape(orientation);
}

举个例子,你可以这样做

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
        if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) 
            return UIInterfaceOrientationIsLandscape(orientation); // If iPad
        else
            return UIInterfaceOrientationIsPortrait(orientation); // Else, it is an iPhone
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2022-11-22
    • 2011-09-27
    相关资源
    最近更新 更多