如何检测 iPhone 5(宽屏设备)? [英] How to detect iPhone 5 (widescreen devices)?

查看:25
本文介绍了如何检测 iPhone 5(宽屏设备)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到 Xcode 4.5 GM,发现您现在可以应用4"Retina 大小到故事板中的视图控制器.

I've just upgraded to Xcode 4.5 GM and found out that you can now apply the '4" Retina' size to your view controller in the storyboard.

现在,如果我想创建一个同时在 iPhone 4 和 5 上运行的应用程序,当然我必须构建每个窗口两次,但我还必须检测用户是否拥有 3.5 英寸的 iPhone.或 4"屏幕,然后应用视图.

Now if I want to create an application that runs on both iPhone 4 and 5, of course I have to build every window twice, but I also have to detect whether the user has an iPhone with 3.5" or 4" screen and then apply the view.

我该怎么做?

推荐答案

首先,您不应该重建所有视图以适应新屏幕,也不应该为不同的屏幕尺寸使用不同的视图.

First of all, you shouldn't rebuild all your views to fit a new screen, nor use different views for different screen sizes.

使用 iOS 的自动调整大小功能,让您的视图可以调整和适应任何屏幕尺寸.

Use the auto-resizing capabilities of iOS, so your views can adjust, and adapt any screen size.

这并不难,阅读一些文档.它会为您节省大量时间.

That's not very hard, read some documentation about that. It will save you a lot of time.

iOS 6 还为此提供了新功能.
请务必阅读 Apple 开发者网站上的 iOS 6 API 更新日志.
并检查新的 iOS 6 AutoLayout 能力.

iOS 6 also offers new features about this.
Be sure to read the iOS 6 API changelog on Apple Developer website.
And check the new iOS 6 AutoLayout capabilities.

也就是说,如果你真的需要检测 iPhone 5,你可以简单地依赖 屏幕尺寸.

That said, if you really need to detect the iPhone 5, you can simply rely on the screen size.

[ [ UIScreen mainScreen ] bounds ].size.height

iPhone 5 的屏幕高度为 568.
你可以想象一个宏来简化这一切:

The iPhone 5's screen has a height of 568.
You can imagine a macro, to simplify all of this:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

fabs 与 epsilon 一起使用是为了防止在比较浮点时出现精度错误,正如 H2CO3 的评论中所指出的那样.

The use of fabs with the epsilon is here to prevent precision errors, when comparing floating points, as pointed in the comments by H2CO3.

所以从现在开始你可以在标准的 if/else 语句中使用它:

So from now on you can use it in standard if/else statements:

if( IS_IPHONE_5 )
{}
else
{}

编辑 - 更好的检测

正如某些人所说,这只会检测宽屏,而不是真正的 iPhone 5.

As stated by some people, this does only detect a widescreen, not an actual iPhone 5.

iPod touch 的下一个版本可能也会有这样的屏幕,所以我们可能会使用另一组宏.

Next versions of the iPod touch will maybe also have such a screen, so we may use another set of macros.

让我们重命名原始宏IS_WIDESCREEN:

#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

让我们添加模型检测宏:

And let's add model detection macros:

#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )
#define IS_IPOD   ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] )

这样,我们可以确保我们有 iPhone 型号宽屏,我们可以重新定义 IS_IPHONE_5 宏:

This way, we can ensure we have an iPhone model AND a widescreen, and we can redefine the IS_IPHONE_5 macro:

#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )

另请注意,如@LearnCocos2D 所述,如果应用程序未针对 iPhone 5 屏幕进行优化(缺少 Default-568h@2x.png 图像),则此宏将不起作用,因为屏幕尺寸仍为在这种情况下为 320x480.

Also note that, as stated by @LearnCocos2D, this macros won't work if the application is not optimised for the iPhone 5 screen (missing the Default-568h@2x.png image), as the screen size will still be 320x480 in such a case.

我不认为这可能是个问题,因为我不明白为什么我们要在未优化的应用中检测到 iPhone 5.

I don't think this may be an issue, as I don't see why we would want to detect an iPhone 5 in a non-optimized app.

重要 - iOS 8 支持

在 iOS 8 上,UIScreen 类的 bounds 属性现在反映了设备方向.
很明显,前面的代码不能开箱即用.

On iOS 8, the bounds property of the UIScreen class now reflects the device orientation.
So obviously, the previous code won't work out of the box.

为了解决这个问题,你可以简单地使用新的 nativeBounds 属性,而不是 bounds,因为它不会随着方向而改变,而且它是基于在纵向模式下.
请注意,nativeBounds 的尺寸以像素为单位,因此对于 iPhone 5,高度将为 1136 而不是 568.

In order to fix this, you can simply use the new nativeBounds property, instead of bounds, as it won't change with the orientation, and as it's based on a portrait-up mode.
Note that dimensions of nativeBounds is measured in pixels, so for an iPhone 5 the height will be 1136 instead of 568.

如果您还针对 iOS 7 或更低版本,请务必使用功能检测,因为在 iOS 8 之前调用 nativeBounds 会使您的应用崩溃:

If you're also targeting iOS 7 or lower, be sure to use feature detection, as calling nativeBounds prior to iOS 8 will crash your app:

if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] )
{
    /* Detect using nativeBounds - iOS 8 and greater */
}
else
{
    /* Detect using bounds - iOS 7 and lower */
}

您可以通过以下方式调整以前的宏:

You can adapt the previous macros the following way:

#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON )
#define IS_WIDESCREEN      ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_WIDESCREEN_IOS8 : IS_WIDESCREEN_IOS7 )

显然,如果您需要检测 iPhone 6 或 6 Plus,请使用相应的屏幕尺寸.

And obviously, if you need to detect an iPhone 6 or 6 Plus, use the corresponding screen sizes.

这篇关于如何检测 iPhone 5(宽屏设备)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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