7

使用中の iPhone のタイプに基づいてビュー レイアウトに多くの変更を加えたいアプリケーションをセットアップしています。1136具体的には、縦画面の解像度が(iPhone5) なのか960(iPhone4)なのか知りたいです。私がやりたいことの 1 つは、UITableView で UITableViewCells の高さを調整して、どちらの電話でもアプリを実行したときに部分的なセルが表示されないようにすることです。

私の質問は、使用中の電話機のハードウェア タイプを検出して、ビュー レイアウトを適切に変更できるようにする最善の方法は何ですか?

4

5 に答える 5

16

私はiphone 5を持っていませんが、他の電話ではこのコードはうまくいきました:

NSMutableDictionary *devices = [[NSMutableDictionary alloc] init];
[devices setObject:@"simulator"                     forKey:@"i386"];
[devices setObject:@"iPod Touch"                    forKey:@"iPod1,1"];
[devices setObject:@"iPod Touch Second Generation"  forKey:@"iPod2,1"];
[devices setObject:@"iPod Touch Third Generation"   forKey:@"iPod3,1"];
[devices setObject:@"iPod Touch Fourth Generation"  forKey:@"iPod4,1"];
[devices setObject:@"iPhone"                        forKey:@"iPhone1,1"];
[devices setObject:@"iPhone 3G"                     forKey:@"iPhone1,2"];
[devices setObject:@"iPhone 3GS"                    forKey:@"iPhone2,1"];
[devices setObject:@"iPad"                          forKey:@"iPad1,1"];
[devices setObject:@"iPad 2"                        forKey:@"iPad2,1"];
[devices setObject:@"iPhone 4"                      forKey:@"iPhone3,1"];
[devices setObject:@"iPhone 4S"                     forKey:@"iPhone4"];
[devices setObject:@"iPhone 5"                      forKey:@"iPhone5"];

- (NSString *) getDeviceModel
{
    struct utsname systemInfo;
    uname(&systemInfo);
    return [devices objectForKey:[NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]];
}

インポートすることを忘れないでください:

#import "sys/utsname.h"
于 2012-10-24T09:29:28.520 に答える
8

画面のフォーマットに合わせてビューを適応させるには、自動レイアウト機能を実際に使用する必要があります。

ただし、デバイスが iPhone 5 (つまり、高さが 1136px) であるかどうかだけを知りたい場合は、次のように使用できます。

[ [ UIScreen mainScreen ] bounds ].size.height

もちろん、マクロに変換できます。

于 2012-10-24T09:30:27.190 に答える
2
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

このマクロをconstant.hファイルで定義し、チェックする場所で使用します

if(IS_IPHONE5)
{
// iphone 5
}
else
{
// earlier
}
于 2012-10-24T09:30:10.320 に答える
2

あなたの質問から、デバイスと解像度に応じて UI を修正したいようです。その場合は問題なく、以下のコードを使用してデバイスを識別できます。ただし、あるデバイスには存在し、他のデバイスには存在しないデバイス機能を探すなど、UI 以外のものについては、どのデバイス モデルであるかではなく、その機能が存在するかどうかを常に確認することをお勧めします。アップルの公式フォーラムより。(ログインが必要です)

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
  if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
    CGSize result = [[UIScreen mainScreen] bounds].size;
    CGFloat scale = [UIScreen mainScreen].scale;
    result = CGSizeMake(result.width * scale, result.height * scale);

    if(result.height == 960){
      NSLog(@"iphone 4, 4s retina resolution");
    }
    if(result.height == 1136){
      NSLog(@"iphone 5 resolution");
    }
  }else{
    NSLog(@"iphone standard resolution");
  }
}else{
  if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)]) {
    NSLog(@"ipad Retina resolution");
  }else{
    NSLog(@"ipad Standard resolution");
  }
}
于 2012-10-24T09:30:15.940 に答える
1

アスペクト比をチェックしています。他のいくつかの方法ほど一般的ではありませんが、特定の解像度をチェックするよりも将来のデバイスに対してより柔軟です。

// 0.563380 = 16x9 (iPhone 5)
// 0.666667 = 3x2  (iPhone 4S and previous)
CGFloat aspectRatio = [UIScreen mainScreen].bounds.size.width / [UIScreen mainScreen].bounds.size.height;
// Tall Screen
if (aspectRatio > 0.55 && aspectRatio < 0.57) {
    // Tall screen stuff.
    }

    // Short Screen
} else if (aspectRatio > 0.64 && aspectRatio < 0.68) {

    // Short screen stuff.
    }
}
于 2012-10-24T17:24:25.290 に答える