user905582
user905582

Reputation: 524

How to differentiate iPhone and iPad in universal application?

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

Is this approach correct?

Upvotes: 9

Views: 2529

Answers (3)

kiran
kiran

Reputation: 4409

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

     Console.WriteLine("Phone");

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

     Console.WriteLine("Pad");

}

Upvotes: 0

user843337
user843337

Reputation:

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.

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

Upvotes: 17

Nick Bull
Nick Bull

Reputation: 4276

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

Upvotes: 2

Related Questions