以编程方式检测iPad / iPhone硬件的最佳方式 [英] Best way to programmatically detect iPad/iPhone hardware

查看:152
本文介绍了以编程方式检测iPad / iPhone硬件的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的是,在iPad上,UIPickerView在横向方向上的高度与在纵向方向上相同。在iPhone上它是不同的。 iPad编程指南为UIDevice引入了一个成语值:

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad programming guide introduces an "idiom" value to UIDevice:

    UIDevice* thisDevice = [UIDevice currentDevice];
    if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        // iPad
    }
    else
    {
        // iPhone
    }

当你在iPad(3.2)而不是iPhone(3.1.3)时工作正常) - 所以看起来还需要ifdef来有条件地编译该检查,例如:

which works OK while you're in iPad (3.2) but not iPhone (3.1.3) - so it looks like there also needs to be an ifdef to conditionally compile that check, like:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
        UIDevice* thisDevice = [UIDevice currentDevice];
        if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
        {
            // etc.
        }
#endif

对我而言,开始看起来非常笨拙。什么是更好的方式?

To me that's starting to look very clumsy. What's a better way?

推荐答案

我现在正在回答这个问题(并且在这个较晚的日期),因为现有的许多答案都是根据Apples当前的文档(iOS 8.1,2015),最高投票实际上看起来是错误的!

I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!

为了证明我的观点,这是来自苹果头文件(总是查看Apple源代码和标题):

To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):

/*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的方法如下:

Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:

1)在iOS PRIOR 版本为3.2的情况下,使用Apple提供的宏:

1) On versions of iOS PRIOR to 3.2, use the Apple provided macro:

// for iPhone use UIUserInterfaceIdiomPhone
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

2)在iOS 3.2或更高版本上,使用[UIDevice currentDevice]上的属性:

2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:

// for iPhone use UIUserInterfaceIdiomPhone
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)

这篇关于以编程方式检测iPad / iPhone硬件的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆