如何将应用程序的方向锁定为纵向 iPhone *仅*?

How to lock application's orientation to portrait for iPhone *only*?

我知道有一些与此相关的线索,但我也无法弄清楚我的具体情况....

我只想将 iPhone 的方向锁定为纵向(并且 iPad 允许横向和纵向,并且不允许上下颠倒)...

尝试了一些代码并将其粘贴到视图控制器、应用程序 delegate.swift 和表视图控制器文件中:

我不知道为什么它不起作用。我正在尝试使用代码来应用更改。我已经阅读了多个主题,但无法弄清楚我的具体情况...

谢谢!

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            if UIDevice.current.userInterfaceIdiom == .phone {
                return UIInterfaceOrientationMask.portrait
            } else {
                return UIInterfaceOrientationMask.allButUpsideDown //return the value as per the required orientation
            }
        }

    }

    func supportedInterfaceOrientations(for window: UIWindow?) -> UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return UIInterfaceOrientationMask.portrait
        } else {
            return UIInterfaceOrientationMask.allButUpsideDown //return the value as per the required orientation
        }
    }

    var shouldAutorotateToInterfaceOrientation: Bool {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return false
        } else {
            return true
        }
    }

    override var shouldAutorotate: Bool {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return false
        } else {
            return true
        }
    }

您不需要任何代码。只需 select Info.plist.

所需的界面方向

根据您的要求,您希望最终结果为:

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

我想为 iPhone 设置纵向模式仅为特定 controller.I 如果你的 viewController 嵌入在情节提要中的导航控制器中设置导航控制器委托 UINavigationControllerDelegate 和添加以下方法

  class ViewController: UIViewController, UINavigationControllerDelegate {
    func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask {

         if (UIDevice.current.userInterfaceIdiom == .phone){
                // Ipad
                 return UIInterfaceOrientationMask.Portrait


            }
                    //Ipad
                     return UIInterfaceOrientationMask.all //What ever condition you want to set for Ipad.
             }
    }