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

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

问题描述

我刚刚升级到XCode 4.5 GM,发现您现在可以将4Retina'尺寸应用到故事板中的视图控制器。

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或4屏幕的iPhone,然后应用查看。

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.

使用自动调整大小

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还提供了相关的新功能,但目前仍在NDA下。

请务必阅读< Apple开发者网站上的一个href =https://developer.apple.com/library/prerelease/ios/#releasenotes/General/iOS60APIDiffs/index.html\"rel =noreferrer> API更改日志,如果你可以访问它。

iOS 6 also offers new features about this, but this is still under NDA at the moment.
Be sure to read the API changelog on Apple Developer website, if you can access to it.

编辑:由于iOS 6现已用完,请检查新的 AutoLayout 功能。

Edit: As iOS 6 is now out, check the new 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

Let's rename the original macro 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型号 AND 宽屏,我们可以重新定义 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.

I不要认为这可能是一个问题,因为我不明白为什么我们想要在非优化的应用程序中检测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上,<$ c $的界限属性c> UIScreen 类现在反映设备方向

很明显,以前的代码无法开箱即用。

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.

最终注释

评论和建议已纳入此帖。

感谢大家。

Comments and suggestions have been incorporated in this post.
Thanks to everybody.

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

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