LoginSignup
10
10

More than 5 years have passed since last update.

画面の向きを固定 2016 swift2 iOS9.2 時点

Posted at

iPhoneの向きに対する制御はiOSのバージョンや言語のバージョンによって制御方法が結構変わっていて混乱したのでこれを書いてる時点での方法をメモ

画面の向きを固定する

    //サポートするデバイスの向きを指定する
    override func supportedInterfaceOrientations()  -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Landscape
    }

AVPlayerViewControllerでは使えません。欲しいのに・・・
上記で固定した場合、TARGETで指定した向きと不一致の場合、エラーで落ちます。
ただし、AppDelegate.swiftで以下を追加した場合はその限りではないです。

    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.AllButUpsideDown
    }

画面の向きが変わった事を検知する

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "onOrientationChange:", name: UIDeviceOrientationDidChangeNotification, object: nil)
    // 端末の向きがかわったら呼び出される.
    func onOrientationChange(notification: NSNotification){

     // 向き毎に制御
     if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)){
        }
   }

もう一つの方法として

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        if size.width > size.height {


        } else {

        }

    }

ただし、上記メソッドは向きが固定されていた場合、呼び出されません。

以上です。またバージョンが変わればここに書いてあるのも使えなくなるかも。

参考サイト

iPhone6Plusと初回起動時の画面向きについて

iOS8以降で、どうデバイスの回転を取り扱うかまとめてみる

より詳しくは上記2サイトを見てもらうのがいいと思う。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10