未分類

マルチデバイスに対応する方法

投稿日:

マルチデバイス対応とは

iPhoneのデバイスによって画面サイズが異なるため、それぞれの端末で正常に表示するための対応。

マルチデバイス対応方法

主に3つの方法がある。

・オートレイアウト
・プログラムで対応
・storyboard分岐

storyboard分岐が楽だが、チームで開発する時は不都合があるため、プログラムで対応する方法も組み合わせるらしい。

storyboard分岐でマルチデバイス対応する方法

対応したいデバイスのstoryboardを追加。

AppDelegate.swiftとSceneDelegate.swiftに以下のメソッドを追記。

func grabStoryboard() -> UIStoryboard{
               
               var storyboard = UIStoryboard()
               let height = UIScreen.main.bounds.size.height
               if height == 667 {
                   storyboard = UIStoryboard(name: "Main", bundle: nil)
                   //iPhone8
               }else if height == 736 {
                   storyboard = UIStoryboard(name: "iPhone8plus", bundle: nil)
                   //iPhone8Plus
               }else if height == 812{
                   storyboard = UIStoryboard(name: "iPhoneXS", bundle: nil)
               }else if height == 896{
                   storyboard = UIStoryboard(name: "iPhoneXSMAX", bundle: nil)
               }else if height == 1112{
                   
                   storyboard = UIStoryboard(name: "iPad", bundle: nil)
               }else{
                   
                   switch UIDevice.current.model {
                   case "iPnone" :
                   storyboard = UIStoryboard(name: "se", bundle: nil)
                       break
                   case "iPad" :
                   storyboard = UIStoryboard(name: "iPad", bundle: nil)
                   print("iPad")
                       break
                   default:
                       break
                   }
               }
               return storyboard
       }

さらにAppDelegate.swiftとSceneDelegate.swiftに以下を追記。

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

※追記する場所はここ↓

シュミレーターでiPhoneXSを選ぶとiPhoneXSの画面が、iPhone8を選ぶとiPhone8の画面が現れるようになる。

-未分類

執筆者:

関連記事

動画制作の基本的な知識

フレームレートとは 1秒間に何コマあるか。 多いと滑らかに動く、少ないとカクカク動く。 基本的に29.97選べばOK。(ほとんどのデバイスはこれで撮影されるから) ちなみに映画やアニメは24で作られて …

no image

クラッシュした時に見るところ・ブレイクポイントを作る

このエラーが出た時に見るところ(選択箇所)↓ reason:のあとを読む。 nextが渡っていないことがわかる。 さらにAutoBreakPointを作ることで、どこでエラーが出ているかわかる。 +→ …

no image

Admobの広告が突然表示されなくなった

何もいじっていないのに、実機で全てのアプリの広告が表示されなくなった。 シュミレーター では表示されている。 コードもAdmobの設定等も変更していない。 Admobで通知も来ていない。 Admob …

自動字幕生成「Vrew」使い方

ファイル→新しい動画で始める で読み込み開始 ショートカット 次のクリップへ移動:tab 前のクリップへ移動:tab + shift 再生/停止:command + P 結合・分割 クリップの分割した …

文字をバラバラに表示させる

バラバラ文字を作る 文字を全選択したら「GG分解」のスクリプトを適用する。 適用すると文字がパーツごとに選択できるようになる。 最終的に表示されるバラバラな状態になるよう位置を調整して、キーを打ってお …