LoginSignup
29
29

More than 5 years have passed since last update.

【Swift】Storyboardをデバイスごとに3.5インチ,4インチで使い分けるスマートな方法

Posted at

はじめに

アプリ申請時に必要そうとなるデバイスによってstoryboardを切り替える方法が意外となかったので書き留めることにしました。

手順

Storyboardの作成

  1. NewFile>iOS>User Interface>Storyboardで、空のStoryboardを作成(ここではMain3.5.storyboardとする) スクリーンショット 2015-04-21 11.26.38.png

  2. 新しいMain3.5.storyboardを開いてView Controllerを追加

  3. そのView Controllerを選択して写真のようにSize>3.5inch用に切り替える。
    スクリーンショット 2015-04-21 11.29.54 1.png

  4. 続けて写真のようにIs Initial View Controllerにチェックを入れる。
    スクリーンショット_2015-04-21_11_31_41.png
    右向き矢印が追加されると思うが、追加しないと立ち上がったときにどのView Controllerを使用すれば良いのかわからなくなりエラーを起こす("– perhaps the designated entry point is not set?")

以上でStory Board の設定は完了である。

AppDelegateへコードの追加

AppDelegate.swift
   func grabStoryboard() -> UIStoryboard {
        var storyboard = UIStoryboard()
        var height = UIScreen.mainScreen().bounds.size.height

        if height == 480 {
            storyboard = UIStoryboard(name: "Main3.5", bundle: nil)
        } else {
            storyboard = UIStoryboard(name: "Main", bundle: nil)
        }
        return storyboard
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        var storyboard: UIStoryboard = self.grabStoryboard()
        if let window = window { 
        window.rootViewController = storyboard.instantiateInitialViewController() as? UIViewController 
        }

        self.window?.makeKeyAndVisible()

        return true
    }

不備やこのほうがいいなどアドバイスがあればコメントしていただけると幸いです。まだまだ未熟者ですのでどうぞ宜しくお願い致します!!!

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