LoginSignup
4
4

More than 5 years have passed since last update.

iPhoneかiPadか区別し、画面の向きを変更する

Posted at

アプリをビルドした時に、iPhoneは縦向き固定。iPadは、右左の横向き固定にしたい。

TARGETSのGeneralにある、Device Interfaceは、どれにもチェックしていません。

以下の記述で対応可能

ViewController.swift
    // デバイスの向き変更
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        var orientation = UIInterfaceOrientationMask()
        // iPhoneの時は縦固定、iPadの時は、横向きの右左固定
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            orientation = UIInterfaceOrientationMask.Portrait // 縦
        } else {
            orientation = [UIInterfaceOrientationMask.LandscapeRight,UIInterfaceOrientationMask.LandscapeLeft] // 右、左
        }
        return orientation
    }

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

NavigationControllerを使っている場合は、上記の記述をそちらに書くとうまくいきます。

4
4
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
4
4