锁定方向不适用于ipad ios 8 swift xcode 6.2

MBH*_*MBH 26 iphone ios swift

我将应用方向锁定为像这样的常规应用设置中的纵向

肖像

但是当我在ipad上运行我的应用程序时,它会旋转!?

那么我应该在哪里锁定方向?

为什么苹果将此功能添加到设置中,而它不起作用!?

更新:

我试图将这个viewController实现为所有viewController的通用.

import Foundation
import UIKit

class GeneralViewController: UIViewController {

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        let value = UIInterfaceOrientation.Portrait.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }

    override func supportedInterfaceOrientations() -> Int {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    }


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

    override func shouldAutorotate() -> Bool {
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

这张照片取自我的应用程序的主导航控制器.

回答1的Pic

还是没用!

这个东西怎么样?

Moh*_*idy 100

你不需要额外的代码,我正在研究Xcode 7.0.1,我注意到当我将屏幕方向锁定为只有肖像时,它只能锁定在iPhone中,也可以将其锁定在iPad中,你需要从设备中选择iPad (而不是通用)并检查肖像并取消选中其他人然后再将设备返回到Universal,因为我发现Universal只反映在iPhone中,它可能是Xcode中的某种错误.

  • 哇,那太奇怪了......谢谢你们分享穆罕默德! (2认同)
  • 绝对好的答案.我们应该怎么知道呢?:d.这是一个很好的发现. (2认同)
  • 我不能赞成这一点.我正在努力解决这个问题. (2认同)
  • 不敢相信到目前为止它仍然是一个错误(在 Xcode 10.1 上测试) (2认同)

Leo*_*bus 7

您可以覆盖视图控制器属性 shouldAutorotate

override var shouldAutorotate: Bool {
    return false 
}
Run Code Online (Sandbox Code Playgroud)

如果视图控制器嵌入在导航控制器中,则可以创建自定义导航控制器:

class PortraitNavigationController: UINavigationController {

    override var shouldAutorotate: Bool {
        return false 
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
Run Code Online (Sandbox Code Playgroud)


小智 7

我也有同样的问题.对我有用的是 - >在设备类型中选择iPad而不是通用 - >取消选中除了纵向 - >再选择全局以外的其他三个选项.完成.


Dav*_*ens 5

这将是添加到 Mohammed Elrashidy 解决方案中的评论,但我没有发表评论的声誉。我正在使用 XCode 9.1 和 Swift 3。

我的应用程序需要一些图形屏幕自动旋转,但所有其他屏幕保持固定。我的问题是“常规”>“部署信息”中的“iPad”设备配置与 iPhone 和通用设备的配置不一致。我在 iPhone 和 Universal 上检查了“纵向”、“向左横向”和“向右横向”,但在 iPad 上检查了所有方向(包括“颠倒”)。只需取消选中“Upside Down”,iPad 设备就会遵循我的 Swift 逻辑中的 shouldAutorotate 和supportedInterfaceOrientations 调用。

我的 Info.plist 现在如下所示,我可以通过编程方式打开或关闭 iPad 的自动旋转。

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
Run Code Online (Sandbox Code Playgroud)