在iOS 10和Swift 2.3中的supportedInterfaceOrientations

5
我正在使用Xcode 8 GM,需要更新一个旧项目以适应iOS 10。我发现,当前使用Swift 2.2版本的App Store构建,在iOS 10上运行时不支持所需的界面方向功能。简单来说,当我覆盖supportedInterfaceOrientations()方法时,在iOS 10上它从未被调用过,但在之前的版本中可以正常工作。我注意到函数签名已经改变,supportedInterfaceOrientations现在是变量而不是方法,但这只适用于Swift 3而不是Swift 2.3。当我尝试在Swift 2.3中覆盖该变量时,它无法编译,如果我使用旧的方法,则在iOS 10下它永远不会被调用。有什么建议吗?

我也遇到了类似的问题。到目前为止有什么进展吗? - Madhan Mani
2个回答

4
尝试以下方法:
1)创建自定义 NavigationController CustomNavigationController.swift:
class CustomNavigationController: UINavigationController, UINavigationControllerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    internal override func shouldAutorotate() -> Bool {
        return visibleViewController!.shouldAutorotate()
    }

    internal override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
      if(visibleViewController?.supportedInterfaceOrientations() != nil){
        return (visibleViewController?.supportedInterfaceOrientations())!
      }else{
        return UIInterfaceOrientationMask.Portrait
      }
    }
}

2) 当您想要在ViewController.swift上进行覆盖时:

override internal func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.Landscape]
    return orientation
}

override internal func shouldAutorotate() -> Bool {
        return false;
    }

4

iOS 10, Swift 3.0版本

override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.all
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接