iPhoneの画面を逆さにも対応させたい時にDevice Orientationを変えるだけではできなかったので、のやり方をまとめます。
普通にやると
通常はプロジェクト作成するとDevice Orientationのうち以下3つがデフォルトでチェックされています。
・Portrait(縦)
・Landscape Left(横向き 左回転)
・Landscape Right(横向き 右回転)
逆向きも対応したい場合は
・Upside Down(縦 逆向き)
にもチェックを入れればOKかと思いきやそうもいきません。
この状態で実行すると逆向きになってくれません。↓
こうやるとできる
ViewControllerでメソッドを1つオーバーライドすると逆向きにも対応できます。
ViewController.swift
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
// 許可する画面を追加
let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait,
UIInterfaceOrientationMask.PortraitUpsideDown,
UIInterfaceOrientationMask.LandscapeLeft,
UIInterfaceOrientationMask.LandscapeRight]
return orientation
}
```
これで実行するとうまくいきます↓
