環境
- Swift4.1
- x-code9.4.1(10.1でも動くことを確認しました)
やりたかったこと
- 端末(iPhone or iPad)によって画面の向きを切り替える
- 条件:iPhoneとiPadで使用する素材が異なる
やったこと
- アプリ起動時に端末を判定→iPhone or iPadで使用するStoryboardを切り替えました
実装内容
①AppDelegateに実装
まず、AppDelegateで以下を実装しました。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
/// 端末の種類
static var deviceType = 0
/// 端末の種類(iPhone、iPad)
enum DeviceType: Int {
case phone = 0,
pad = 1
}
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if AppDelegate.deviceType == DeviceType.pad.rawValue {
print("iPadだよ!")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Tablet", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "rootView")
self.window?.rootViewController = initialViewController
// iPad用のstoryboardを使用
self.window?.makeKeyAndVisible()
} else {
print("iPhoneだよ!")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "rootView")
self.window?.rootViewController = initialViewController
// iPhone用のstoryboardを使用
self.window?.makeKeyAndVisible()
}
return true
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
/// 端末の向きを指定
switch UIDevice.current.userInterfaceIdiom {
case .phone:
// iPhoneは縦向き固定
AppDelegate.deviceType = DeviceType.phone.rawValue
return UIInterfaceOrientationMask.portrait
case .pad:
// iPadは横向き固定
AppDelegate.deviceType = DeviceType.pad.rawValue
return UIInterfaceOrientationMask.landscape
default:
return UIInterfaceOrientationMask.portrait
}
}
}
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
は、ViewControllerに使用するインターフェイスの向きをデリゲートに要求するメソッドです。
②Storybordの設定
次に、storybordの設定をします。
基本的なことですが、Class(ViewController)と、storyboardIdの設定も忘れずに・・・!
今回は、iPhone用のstorybordを「Main」、iPad用を「Tablet」という名前にしました。
(iPadのiが大文字になるのが個人的に気持ち悪くてTabletにしました・・・iPadかTabletで統一した方がいいと思うので、Padにすればよかったですかもね・・・)
上でも書きましたが、端末によってstoryboardを分けているのは、
使用する素材(tabbarに使用する画像とか)が別物だったためです。
(今回は、tabbarの色を変えてみましたw)
iPhoneを縦向き固定に、iPadを横向き固定にしたいので、
わかりやすいようにstoryboard上でもiPadは横向きにしておきます。
端末の設定方法ですが、「View as」
をクリックすると
storyboard上の表示内容を変更できます。
「Device」で端末選択、「Orientation」で画面の向きを選択できます。
このまま起動しても
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
が呼ばれず、
端末の判定ができないので、設定周りを変更します。
③設定周りを変更
デフォルトでは、赤枠で囲ってある部分の「Requires full screen」
のチェックが外れていると思うので、「Requires full screen」
にチェックを入れます。
これで、端末判定がコード上でできるようになりました。
念のため、DeviceがUniversalになっていることも確認しておくと良いでしょう。
Device Orientationでも、端末の向きを固定に設定にできるみたいですが、
今回は、PortraitとLandScapeの両方にチェックを入れています。
iOS9にしたiPadには画面左側と右側で別々のアプリを起動するという機能があるようです。Requires full screen とはその機能を使うかどうかという設定をできるものでした。
引用元:https://www.clrmemory.com/mac/xcode-error-90474/
実行結果
端末に応じて、画面の向きとStorybordを切り替えることができるようになりました!