iphone - 以编程方式检测 iPad/iPhone 硬件的最佳方法

标签 iphone orientation ipad

我需要找出的原因是,在 iPad 上,UIPickerView 在横向和纵向上具有相同的高度。在 iPhone 上情况有所不同。 iPad 编程指南向 UIDevice 引入了一个“idiom”值:

    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

对我来说,这开始看起来非常笨拙。有什么更好的方法吗?

最佳答案

运行时检查(第一种方法)与编译时的 #if 完全不同。预处理器指令不会为您提供通用应用程序。

首选方法是使用 Apple 的宏:

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 之前未定义宏),您可以定位之前的操作系统版本以使其在 iPhone 上运行。

关于iphone - 以编程方式检测 iPad/iPhone 硬件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2862140/

相关文章:

iphone - UIInterfaceOrientation 更改在 subview 中不起作用

swift - 在 iPad 上全屏显示 Swift Playgrounds

iphone - 如何将自定义菜单项添加到 UITextView 菜单,这是指向所选单词的维基百科页面的链接?

android - Android 中基于 CelID 的位置?

iphone - 我应该使用哪些搜索和比较选项来搜索汉字?

iphone - 将 iPhone 应用程序与 Facebook Twitter TwitPic 等集成

javascript - iOS 网络应用程序 : Rotate the body with the orientation change

ios - 旋转呈现 View 并锁定呈现 View Controller 的方向

android - 旋转设备时磁场测量的奇怪行为

objective-c - iPad 输入密码 View