如何在通用应用中区分iPhone和iPad?

时间:2012-01-06 10:56:47

标签: iphone ios ipad

我想区分iPhone和iPad的控制器。

        #ifdef __IPHONE_NA
            {

            UINavigationBar *ipadNavBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 768.0f, 50.0f)];
            [[self view] addSubview: ipadNavBar];

            UINavigationItem *ipadNavItem = [[UINavigationItem alloc] initWithTitle: @"EMPLOYEE"];
            [ipadNavBar pushNavigationItem:ipadNavItem animated:NO];
            }
    else 
        {

        UINavigationBar *ipadNavBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 360.0f, 45.0f)];
        [[self view] addSubview: ipadNavBar];



UINavigationItem *ipadNavItem = [[UINavigationItem alloc] initWithTitle: @"EMPLOYEE"];
    [ipadNavBar pushNavigationItem:ipadNavItem animated:NO];
    }

如果说错误未终止#ifdef

这种做法是否正确?

3 个答案:

答案 0 :(得分:17)

您可以使用已存在的常量:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    // Some code for iPhone
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    // Some code for iPad
}

当然你不需要else if语句,你可以只使用else但是我只是用它来说明可用的差异常数。

您可以找到更多here(请参阅UI_USER_INTERFACE_IDIOM部分)。

答案 1 :(得分:2)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        NSLog(@"iPad Idiom"); 
    else
        NSLog(@"iPhone Idiom");

答案 2 :(得分:0)

if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {

     Console.WriteLine("Phone");

} else if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {

     Console.WriteLine("Pad");

}
相关问题