我需要了解的原因是,在iPad上,UIPickerView在横向方向上具有与在纵向方向相同的高度。在iPhone上它是不同的。 iPad编程指南为UIDevice引入了“成语”值:
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// iPad
}
else
{
// iPhone
}
当你在iPad(3.2)而不是iPhone(3.1.3)时工作正常 - 所以看起来还需要有一个ifdef来有条件地编译该检查,例如:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// etc.
}
#endif
对我而言,开始看起来非常笨拙。什么是更好的方式?
答案 0 :(得分:63)
在运行时检查(第一种方式)与编译时的#if完全不同。预处理程序指令不会为您提供通用应用程序。
首选方法是使用Apple的Macro:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
使用3.2作为基本SDK(因为宏未在3.2之前定义),您可以定位以前的OS版本以使其在iPhone上运行。
答案 1 :(得分:26)
我现在回答这个问题(并且在这个较晚的日期)因为许多现有的答案已经很老了,根据Apples当前的文档(iOS 8.1,2015),最多Up Voted实际上似乎是错误的!
为了证明我的观点,这是来自Apples头文件的注释(总是查看Apple源代码和标题):
/*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
deploying to a version of the iOS less than 3.2. If the earliest
version of iPhone/iOS that you will be deploying for is 3.2 or
greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
因此,目前 APPLE推荐检测iPhone与iPad的方法如下:
1)在iOS PRIOR 到3.2的版本上,使用Apple提供的宏:
// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
2)在iOS 3.2或更高版本的版本上,使用[UIDevice currentDevice]上的属性:
// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
答案 2 :(得分:17)
我喜欢我的isPad()功能。相同的代码,但只在一个地方保持不见。
答案 3 :(得分:15)
我的解决方案(适用于3.2 +):
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
然后,
if (IS_IPAD)
// do something
或
if (IS_IPHONE)
// do something else
答案 4 :(得分:1)
在Swift中使用userInterfaceIdiom实例属性as-
if UIDevice.current.userInterfaceIdiom == .phone {
print("iPhone")
}
&安培;对于其他设备 -
switch UIDevice.current.userInterfaceIdiom {
case .pad:
print("iPad")
case .phone:
print("iPhone")
case .tv:
print("TV")
case .carPlay:
print("carPlay")
default: break;
}
答案 5 :(得分:0)
将此方法放入App Delegate中,以便您可以使用[[[UIApplication sharedApplication] delegate] isPad]在任何地方调用它
-(BOOL)isPad
{
BOOL isPad;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
if(range.location==NSNotFound)
{
isPad=NO;
}
else {
isPad=YES;
}
return isPad;
}
答案 6 :(得分:0)
如果您使用的是向后兼容的功能,我发现最好的方法是在预编译的头文件中创建一个#define。例如:
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_2
#define USING_4_X
#endif
然后在您的代码中,您可以执行此操作:
BOOL exists = NO;
#ifdef USING_4_X
exists = [SomeObject someMethod:[url lastPathComponent]];
#else
exists = [SomeObject someMethod:[[url path] lastPathComponent]];
#endif
答案 7 :(得分:0)
如果 1-您已将应用程序安装到您的设备中, 2-您将其构建设置更改为“通用”应用程序, 3-在预先存在的应用程序之上将应用程序安装到您的设备上(不删除之前的应用程序)
您可能会发现此处提供的用于检测iPhone / iPad的解决方案不起作用。首先,删除仅适用于iPad / iPhone的应用程序并将其全新安装到您的设备上。
答案 8 :(得分:0)
BOOL isIpad()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return NO;
}
答案 9 :(得分:0)
extension UIDevice {
var isIPad: Bool {
return UIDevice.current.userInterfaceIdiom == .pad
}
}