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

时间:2021-03-29 22:51:49

I want to differentiate the controller for iPhone and 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];
    }

if saying error unterminated #ifdef

如果说错误没有终止#ifdef

Is this approach correct?

这种方法是否正确?

3 个解决方案

#1


17  

You can make use of the constants already present:

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

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

Naturally you wouldn't need the else if statement, you could just use else however I'm just using it illustrate the difference constants available.

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

You can find out more here (look at the section on UI_USER_INTERFACE_IDIOM).

您可以在此处找到更多信息(请参阅UI_USER_INTERFACE_IDIOM部分)。

#2


2  

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

#3


0  

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

     Console.WriteLine("Phone");

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

     Console.WriteLine("Pad");

}

#1


17  

You can make use of the constants already present:

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

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

Naturally you wouldn't need the else if statement, you could just use else however I'm just using it illustrate the difference constants available.

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

You can find out more here (look at the section on UI_USER_INTERFACE_IDIOM).

您可以在此处找到更多信息(请参阅UI_USER_INTERFACE_IDIOM部分)。

#2


2  

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

#3


0  

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

     Console.WriteLine("Phone");

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

     Console.WriteLine("Pad");

}