mzgkworks

iOSを中心にプログラミング関係について書く

【Swift】ViewController単位に画面の向きを固定する方法

ある1画面だけ横向き(LandscapeRight)にしたかったので、その指定方法。
画面はすべてSingleViewで作成。

環境

向きを固定して自動回転

各viewController.swiftに以下を記述する。
TARGETS - Development Info - Device Orientation はデフォルトのままにしておく。

// MARK: - デバイスの向き(横のみの場合)
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    let orientation: UIInterfaceOrientationMask = UIInterfaceOrientationMask.LandscapeRight
    // 複数許す場合
    // let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown]
    return orientation
}

//指定方向に自動的に変更
override func shouldAutorotate() -> Bool{
    return true
}