Wie kann ich die Ausrichtung in Swift programmgesteuert drehen?

Ich habe es am nächsten versucht:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")

au

viewWillAppear

aber es funktioniert nicht. Wie kann ich meinen Bildschirm auf viewWillAppear drehen?

AKTUALISIERE

Ich verwende den nächsten Code zum Sperren einer Ausrichtung:

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)
    }
}

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)
        }
    }

und in meinem ERSTEN Controller in viewWillAppear setze ich:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = true
}

in meinem zweiten Controller:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = false
}

so, wenn ich vom zweiten Controller zum ersten zurückkehre:

Ich drehe es nach links - dreht sich nicht, dreht sich zurück nach rechts - nichts, dreht sich nach links, noch einmal - es dreht sich.

Wie kann ich es reparieren