如何在通用应用中区分iPhone和iPad?
我想区分iPhone和iPad的控制器如何在通用应用中区分iPhone和iPad?,iphone,ios,ipad,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]; UIN
#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
这种方法正确吗?您可以利用已经存在的常数:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
NSLog(@"iPad Idiom");
else
NSLog(@"iPhone Idiom");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
// Some code for iPhone
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Some code for iPad
}
当然,您不需要else if
语句,您可以使用else
但是我只是用它来说明可用的差异常量
您可以找到更多信息(请参阅
UI\u用户界面\u惯用语
)部分。好的答案与代码示例一起提供,并为将来的读者提供解释。虽然问这个问题的人可能理解你的答案,但解释一下你是如何得出这个答案的将有助于无数其他人。
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
Console.WriteLine("Phone");
} else if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
Console.WriteLine("Pad");
}