First of all, you should not rebuild all their views to fit a new screen, or the use of different views for different screen sizes.
The use of auto-resizing capabilities of iOS, so their opinion can adjust and adapt to any screen size.
That is not very difficult to read some documentation about it. This will save a lot of time.
iOS 6 also offers new features on this but this is still the NDA at the time.
Be sure to read the list of changes API Apple Web Developer, if you can access it.
Edit : As iOS 6 already, check the new design Automatic capabilities.
That said, if you really need to detect the iPhone 5, you can simply rely on the size of the screen .
[[UIScreen mainScreen] bounds ] .size .height
The iPhone 5 is the screen has a height of 568.
You can imagine a macro to simplify all this:
# define IS_IPHONE_5 (fabs ((double) [[UIScreen mainScreen] bounds] .size.height - (double) 568) <DBL_EPSILON)
Using fabs
with epsilon is here to avoid mistakes precision floating point when compared, as noted in the comments by H2CO3.
So from now on you can use the if / else norm:
if (IS_IPHONE_5)
{}
else
{}
Edit - Better detection
As stated by some people, this not only detect a screen , not a real iPhone 5.
The next version of the iPod touch may also have a screen, so you can use another set of macros.
Let's rename the original macro IS_WIDESCREEN
:
# define IS_WIDESCREEN (fabs ((double) [[UIScreen mainScreen] bounds] .size.height - (double) 568) <DBL_EPSILON)
And we will add detection model macros:
# define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString: @ "iPhone"])
# define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString: @ "iPod touch"])
In this way, we ensure that we have a model of iPhone and a screen, and we can redefine theIS_IPHONE_5
macro:
# define IS_IPHONE_5 (IS_IPHONE && IS_WIDESCREEN)
Also note that, as said @ LearnCocos2D, that macros do not work if the app is not optimized for iPhone 5 screen (missing [email protected] image), as the size of the screen will still be 320x480 in this case.
I do not think this can be a problem because you do not see why we would want to detect an iPhone 5 in a non-optimized application.
IMPORTANT - iOS 8
In iOS 8 bounds
of ownership UIScreen
class now reflects the orientation of the device .
So obviously, the above code does not work out of the box.
To fix this, you can simply use the new nativeBounds
of the property, instead of bounds
, as it will not change the orientation, and as based on a portrait mode.
Note that the dimensions of nativeBounds
is measured in pixels, so that for an iPhone 5 the height will be 1136 instead of 568.
If you are also targeting iOS 7 or less, be sure to use feature detection, such as calling nativeBounds
before iOS 8 lock your application:
if ( [[UIScreen mainScreen] respondsToSelector : @ selector ( nativeBounds)])
{
}
else
{
}
You can adapt the above macros follows:
#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)
And obviously, if you need to detect an iPhone 6 or 6 Further, using the corresponding display sizes.
Final Note
Comments and suggestions have been incorporated into this post.
Thank you all.