【问题标题】:supportedInterfaceOrientations() error in Swift 2.0Swift 2.0 中的supportedInterfaceOrientations() 错误
【发布时间】:2015-09-10 15:26:07
【问题描述】:

我刚刚将一个项目迁移到 Swift 2.0,这个以前工作的代码现在产生了一个错误:

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

错误表明返回类型不正确,但我尝试了几种返回方法,但都没有成功。

无法将“Int”类型的返回表达式转换为“UIInterfaceOrientationMask”类型的返回

【问题讨论】:

    标签: ios xcode swift


    【解决方案1】:

    从 Swift 2.0 开始,位掩码被使用掩码值数组代替,您的代码现在应为:

     override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [.Portrait, .PortraitUpsideDown]
    }
    

    【讨论】:

    • 非常感谢!迁移后我遇到的所有错误都与位掩码相关,因此这是非常有用的信息。
    • 太棒了,那你能接受答案是正确的吗?
    • 当然,还是要等几分钟才能让我接受。
    • 如果您能帮助解决这个类似的问题,我将不胜感激! stackoverflow.com/questions/32506336/…
    • 在 Swift 3 中?
    【解决方案2】:

    Swift 3.0 的supportedInterfaceOrientations 有更新:

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {  
        return UIInterfaceOrientationMask.portrait  
    }
    

    以及自动旋转功能:

    override var shouldAutorotate: Bool {
        return true
    }
    

    【讨论】:

      【解决方案3】:

      Danny Bravo 的回答是正确的,但我想指出另一件事。从 Swift 2.0 开始,UIInterfaceOrientationMask(和大多数其他位掩码类型)符合OptionSetType,它符合AlgebraSetType,这意味着您可以对unionintersect 等进行各种操作。

      例如,在 Swift 1.2 中,代码如下:

      override func supportedInterfaceOrientations() -> Int {
          var orientations = UIInterfaceOrientationMask.Portrait
          if FeatureManager.hasLandscape() {
              orientations |= UIInterfaceOrientationMask.Landscape
          }
      
          return Int(orientations.rawValue)
      }
      

      ...在 Swift 2.0 中会更像这样:

      override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
          var orientations = UIInterfaceOrientationMask.Portrait
          if FeatureManager.hasLandscape() {
              orientations.insert( UIInterfaceOrientationMask.Landscape )
          }
      
          return orientations
      }
      

      【讨论】:

      • 只是好奇:这种奇特的魅力是延续到 Swift 3 还是再次改变?谢谢!
      猜你喜欢
      • 2016-02-24
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2016-02-23
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多