背景
画面(ViewController)ごとに回転を制御したいとき、
とりあえず回転する可能性のある方向をすべて指定してから
UISupportedInterfaceOrientations
各ViewController
で
shouldAutoroate, supportedInterfaceOrientations
をオーバーライドして制御する方法しか知らなかった。
あまりスマートじゃないと感じたので別の方法を探してみた。
回転制御方法
以下のようにするとViewController
ごとに回転を制御できるようになる。
ソースコード
AppDelegate.swift
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if window?.rootViewController?.presentedViewController is ViewController2 {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
}
ではUISupportedInterfaceOrientationsって?
UISupportedInterfaceOrientations
によると、アプリを起動するときに取りうる方向を指定しておくものらしい。