swift 2.0中的supportedInterfaceOrientations()错误

18 xcode ios swift

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {  
        return Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) | Int(UIInterfaceOrientationMask.Portrait.rawValue)  
    } 
Run Code Online (Sandbox Code Playgroud)

该错误表明返回类型不正确,但我已经尝试了几种方法来返回它,没有运气.

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

Dan*_*avo 44

从Swift 2.0开始,使用掩码值数组替换位掩码,现在应该读取代码:

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return [.Portrait, .PortraitUpsideDown]
}
Run Code Online (Sandbox Code Playgroud)


ale*_*hun 5

有一个Swift 3.0的supportedInterfaceOrientations更新:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {  
    return UIInterfaceOrientationMask.portrait  
}
Run Code Online (Sandbox Code Playgroud)

和自动旋转功能:

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