43
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSデバイスに応じて,使用するストーリーボードを変更する

Posted at

IPhone 4sとiPhone 5の間で,画面の高さが変更になりました.また,Universal Appにする場合に,iPhoneのStoryboardとiPadのStoryboardそれぞれに,ViewControllerを用意するのは無駄です.
そこで,Storyboardを3つ作成して,一つのViewControllerの同一のpropertyやmethodに接続すればデバッグも簡単になります.


仮に3つのStoryboardの名前を

  1. iPad.storyboard
  2. iPhone_old.storyboard
  3. iPhone.storyboard

とします.

AppDelegate.hに

@property (nonatomic) NSString  *sbName;

を定義し,Storyboardの名称を格納する領域を取ります.

AppDelegate.mの@implementation AppDelegate直下に

@synthesize sbName;

を追加します.

didFinishLaunchingWithOptionsを以下の様に書き換えます.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Override point for customization after application launch.
    // StoryBoardの型宣言
    UIStoryboard *storyboard;
     // StoryBoardの名称設定用
    NSString * storyBoardName;
    
    // 機種の取得
    NSString *modelname = [[UIDevice currentDevice] model];
    
    // iPadかどうか判断する
    if ( ![modelname hasPrefix:@"iPad"] ) {
        
        // Windowスクリーンのサイズを取得
        CGRect r = [[UIScreen mainScreen] bounds];
        // 縦の長さが480の場合、古いiPhoneだと判定
        if(r.size.height == 480)
        {
            storyBoardName = @"iPhone_old";
        }
        else
        {
            storyBoardName = @"iPhone";
        }
    }
    else
    {
        storyBoardName = @"iPad";
    }

    sbName = storyBoardName;

    // StoryBoardのインスタンス化
    storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
    
    // 画面の生成
    UIViewController *mainViewController = [storyboard instantiateInitialViewController];
    
    // ルートウィンドウにひっつける
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = mainViewController;
    [self.window makeKeyAndVisible];
    
    return YES;
}

これで3つのStoryboardを用意して,それぞれにレイアウトを工夫して,一つのViewControllerで制御する事が可能になります.

43
39
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
43
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?