Ada*_*ach 46 iphone orientation ipad
我需要找到的原因是,在iPad上,UIPickerView在横向方向上具有与在纵向方向相同的高度.在iPhone上它是不同的.iPad编程指南为UIDevice引入了一个"成语"值:
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
// iPad
}
else
{
// iPhone
}
Run Code Online (Sandbox Code Playgroud)
你在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
Run Code Online (Sandbox Code Playgroud)
对我而言,开始看起来非常笨拙.什么是更好的方式?
Eik*_*iko 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.
}
Run Code Online (Sandbox Code Playgroud)
使用3.2作为基本SDK(因为宏未在3.2之前定义),您可以定位以前的OS版本以使其在iPhone上运行.
Cli*_*udo 28
我现在正在回答这个问题(并且在这个较晚的日期),因为许多现有的答案已经很老了,根据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.*/
Run Code Online (Sandbox Code Playgroud)
因此,目前APPLE推荐的方式来检测iPhone与iPad,如下:
1)在iOS PRIOR 3.2 版本上,使用Apple提供的宏:
// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
Run Code Online (Sandbox Code Playgroud)
2)在iOS 3.2或更高版本上,使用[UIDevice currentDevice]上的属性:
// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
Run Code Online (Sandbox Code Playgroud)
lew*_*son 15
我的解决方案(适用于3.2+):
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
Run Code Online (Sandbox Code Playgroud)
然后,
if (IS_IPAD)
// do something
Run Code Online (Sandbox Code Playgroud)
要么
if (IS_IPHONE)
// do something else
Run Code Online (Sandbox Code Playgroud)
在 Swift 中使用userInterfaceIdiom实例属性作为-
if UIDevice.current.userInterfaceIdiom == .phone {
print("iPhone")
}
Run Code Online (Sandbox Code Playgroud)
& 对于其他设备 -
switch UIDevice.current.userInterfaceIdiom {
case .pad:
print("iPad")
case .phone:
print("iPhone")
case .tv:
print("TV")
case .carPlay:
print("carPlay")
default: break;
}
Run Code Online (Sandbox Code Playgroud)