iOS 8 颠倒方向,启用 XCode 选项,仍然不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27525657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:22:58  来源:igfitidea点击:

iOS 8 upside down orientation, XCode option enabled, still doesn't work

iphonexcodeios8orientation

提问by Cleversou

I have a universal application being developed in iOS8 (XCode 6.1.1). It will support all the 4 orientations (left, right, portrait & upside down).

我有一个在 iOS8 (XCode 6.1.1) 中开发的通用应用程序。它将支持所有 4 个方向(左、右、纵向和倒置)。

The problem is that, although in XCode the four options for supported orientations are checked, only left, right & portrait are working properly. So, is there a bug on XCode or iOS8? My info.plistshows all the supported orientations, but when I run the app on simulator or on the device it doesn't 'flip' the orientation when it's upside down. (PS: it's an singleview app, it doesn't have any navigation controller).

问题是,虽然在 XCode 中检查了支持方向的四个选项,但只有左、右和纵向可以正常工作。那么,XCode 或 iOS8 上是否存在错误?我的info.plist显示了所有支持的方向,但是当我在模拟器或设备上运行应用程序时,当它颠倒时它不会“翻转”方向。(PS:它是一个单视图应用程序,它没有任何导航控制器)。

Thanks!

谢谢!

Cleverson

克莱弗森

采纳答案by Cleversou

Well, I have figured out the purpose of the the options on project configuration... Within the options you're saying "my app support these orientations" and not "my app must use these four options"... So, on iPhone you must explicity say that a particular ViewController support all orientations (the upsidedown doesn't make parte of the default orientations, for a reason that I dont know)... The code should be like this for supporting all orientations:

嗯,我已经弄清楚了项目配置选项的目的......在选项中你说“我的应用程序支持这些方向”而不是“我的应用程序必须使用这四个选项”......所以,在 iPhone 上你必须明确地说一个特定的 ViewController 支持所有方向(颠倒不会使默认方向成为一部分,原因我不知道)......代码应该是这样的以支持所有方向:

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

回答by Aral Balkan

With Swift 2.1, you can simplify @Cleversou's answer to:

使用 Swift 2.1,您可以将@Cleversou 的回答简化为:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
{
  return .All
}

With Swift 2.3

使用 Swift 2.3

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
  return .all
}

回答by siraustin

If you are running inside of a navigation controller or tab bar controller, you will need to do the same override in your subclass or override all instances with an extension:

如果您在导航控制器或标签栏控制器内部运行,则需要在子类中执行相同的覆盖或使用扩展覆盖所有实例:

extension UINavigationController {
  override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .All
  }
}

extension UITabBarController {
  override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .All
  }
}

Edit: as of Swift 3.0 / iOS 9 (and possibly earlier) this would be:

编辑:从 Swift 3.0 / iOS 9(可能更早版本)开始,这将是:

extension UINavigationController {
    override open var supportedInterfaceOrientations : UIInterfaceOrientationMask     {
        return .all
    }
}

extension UITabBarController {
    override open var supportedInterfaceOrientations : UIInterfaceOrientationMask     {
        return .all
    }
}