XcodeでiPhone6/iPhone6 Plusを判別する方法

  •  

 

XcodeでiPhone6/iPhone6 Plusを判別する方法です。
ウィンドウのスクリーンサイズを取得すれば判別が可能です。

コードは以下。
Objective-C

    CGRect r = [[UIScreen mainScreen] bounds];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        if(r.size.height == 480){
            // iPhone4
        } else if(r.size.height == 568){
            // iPhone5
        } else if(r.size.height == 667){
            // iPhone6
        } else if(r.size.height == 736){
            // iPhone7 Plus 
        }
    } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        if (r.size.height == 1024) {
            // iPad
        } else if (r.size.height == 1366) {
            // iPad Pro
        }
    }

Swift

    let r:CGRect = UIScreen.main.bounds
    if UIDevice.current.userInterfaceIdiom == .phone {
        if r.size.height == 480 {
            // iPhone4
        } else if r.size.height == 568 {
            // iPhone5
        } else if r.size.height == 667 {
            // iPhone6
        } else if r.size.height == 736 {
            // iPhone7 Plus 
        }
    } else if UIDevice.current.userInterfaceIdiom == .pad {
        if r.size.height == 1024 {
            // iPad
        } else if r.size.height == 1366 {
            // iPad Pro
        }
    }


複雑なレイアウトのアプリの場合など、画面サイズに合わせてStoryBoardを用意し、AppDelegateでStoryBoardを読み込む際に使っています。

株式会社woodsmallの小林でした。
https://woodsmall.co.jp/

関連記事

コメント

この記事へのコメントはありません。

TOP