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 {
}
}
ただし、上記メソッドは向きが固定されていた場合、呼び出されません。
以上です。またバージョンが変わればここに書いてあるのも使えなくなるかも。
参考サイト
より詳しくは上記2サイトを見てもらうのがいいと思う。