小编Roh*_*hak的帖子

如何在Swift中以编程方式旋转方向?

我尝试了下一个:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

viewWillAppear
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何在viewWillAppear上旋转屏幕?

UPDATE

我使用下一个代码来锁定方向:

var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if shouldRotate == true {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 4.0

  private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
        if shouldRotate == true {
            return Int(UIInterfaceOrientationMask.portrait.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的第一个控制器viewWillAppear我设置:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)

在我的SECOND控制器中:

if let appDelegate = …
Run Code Online (Sandbox Code Playgroud)

orientation ios swift

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

ios ×1

orientation ×1

swift ×1