8

私のアプリには iPhone 5 用の追加機能があり、そのための .xib を持つ別のクラスを作成しました。画面の高さを検出し (デバイス ID/モデルを取得できない場合)、それに応じて別のビュー コントローラーをロードしたいと思います。私はこれを試しました:

- (IBAction)select:(id)sender {

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

if (screenHeight == 960) {

Selection *selectView =[[Selection alloc] initWithNibName:nil bundle:nil];
selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:selectView animated:YES];

}

else {

    Selection_5 *selectView =[[Selection_5 alloc] initWithNibName:nil bundle:nil];
    selectView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:selectView animated:YES];

}

}

Selection と Selection_5 は 2 つの異なるクラスであり、それぞれユーザー インターフェイスの xib が異なります。

4

4 に答える 4

8

まず、デバイスの種類ごとに確認したくありません。新しい iPod touch (画面サイズは同じ) や、来年の iPhone ではどうなるでしょうか。

しかし、ここでの問題は、実際のピクセル数に基づいて画面サイズをチェックしていることだと思いますが、これは奇妙なことに、あなたが望むものではありません。Retina 画面ではすべてが「2 倍」になることに注意してください。UI では、(ほとんどの場合) すべてに「通常の」サイズを使用します。この場合は、ピクセル数の半分です。

つまり、画面の高さが 480 (通常) または 568 (iPhone 5) であることを確認します。

于 2012-10-02T10:30:37.817 に答える
5

http://github.com/erica/uidevice-extension/を試してください

[[UIDevice currentDevice] platformType]   // ex: UIDevice4GiPhone
[[UIDevice currentDevice] platformString] // ex: @"iPhone 4G"

または、次のように screenHeight を見ることができます。

float screenHeight = [UIScreen mainScreen].bounds.size.height;

iPhone 5 の高さは 568

次のような .xib をロードする場合は、シェルを使用して nib を設定することもできます。

[[Selection alloc] initWithNibName:@"here_is_nibname" bundle:nil];
于 2012-10-02T10:18:00.960 に答える
5

私のアプリでは、iPhone、iPhone5/iPod Touch、および iPad 用の .XIB ファイルをロードする必要があります。そのために、これは私が使用するコードです。

// If Iphone/iPod Touch
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  // If iPhone 5 or new iPod Touch
  if([UIScreen mainScreen].bounds.size.height == 568){
     VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerExt" bundle:nil];
     ...
  } else{
    // Regular iPhone
    VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewController" bundle:nil];
     ...          
  }
// If iPad
} else {
  VCDadosViewController *extratoVC = [[VCDadosViewController alloc] initWithNibName:@"VCDadosViewControllerPad" bundle:nil];
  ...
}

必要な人に役立つことを願っています:)

于 2012-11-03T03:42:11.040 に答える
3

If you've got this naming convention

VGArticlePage~ipad.xib
VGArticlePage~iphone.xib
VGArticlePage~iphone_ext.xib

Then you can do like this

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)

- (NSString *)nibNameForClass:(Class)class
{
    if(IS_IPHONE && IS_IPHONE_5)
    {
        return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone_ext"];
    }
    else if(IS_IPHONE)
    {
        return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~iphone"];
    }
    else
    {
        return [NSString stringWithFormat:@"%@%@", NSStringFromClass(class), @"~ipad"];
    }
}
于 2013-01-18T10:34:08.773 に答える