coding for iphone 5 screen - ios

I have taken a universal project. I have coded for iPhone 5 screen but it doesn't detect it.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//ui for ipad
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
tableView.frame = CGRectMake(0, 40, 320 ,508);
}
else {
tableView.frame = CGRectMake(0, 40, 320 ,420);
}
}
It doesn't work though. When I debug the code on iPhone 5 screenBounds = 320*480.
Where am I going wrong?

It is so simple to detect iPhone5 screen,
Just write
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
in your .pch file.... and you can further check for iPhone5 screen like if(IS_IPHONE5).

Choose your UITableView from the IB and select the size as Retina 4 for all screens, even the main window xib file. This works fine even on the iphone 4.

you add Splash Screen for iphone 5.Default-568h#2x.png file then its work fine

This code works fine for me
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
if (IS_IPHONE_5)
   {
//iphone 5
}

Try using this to detect iPhone 5 instead:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize theHeight = [[UIScreen mainScreen] bounds].size;
if(theHeight.height == 568) {
// iPhone 5
tableView.frame = CGRectMake(0, 40, 320 ,508);
}
else {
// iPhone 4
tableView.frame = CGRectMake(0, 40, 320 ,420);
}
}
Alternatively you can define this:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
Which you can re-use whenever you need to find out if it's iPhone 5 or not.

Try this code ::
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
[tableView setFrame:CGRectMake(0, 40, 320 ,508)];
}
else if ([[UIScreen mainScreen] bounds].size.height == 480)
{
[tableView setFrame:CGRectMake(0, 40, 320 ,420)];
}
Hope, it'll help you.
Thanks.

I used this code to check iPhone 5 device.
float scrnheight = [[UIScreen mainScreen] bounds].size.height ;
if(scrnheight == 568.0f)
{
self.isIphone5 = true;
}

Related

How can you tell if a user is using an iPhone 6 or 6+

Forgive me if this is a really stupid question I am just a beginner at all of this stuff. I want to make a separate interface for the iPhone 6 and 6 Plus but I don't know how the best way to be going about this is. This is a very simple app just to get myself more familiar with the language, etc. I was wondering if there is an easy way to tell if a user is using a 6 or 6 plus to make a separate interface for them. Thanks :)
Use single storyboard. Learn autolayout from raywanderlich, it think its a very good resource.
Also,now take advantage of the size classes introduced for ios 8 in xcode 6. This is called adaptive layout.
So instead of making the above four views for 3.5, 4, 4.7, 5.5 inch devices separately, and then worry about combining their respective landscape modes as well, just go for the 4 combination of size classes using auto layout (if possible, highly recommended) and relax for any other device size that may come into future.
To detect iPhone Version Please use below code.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 667){
// iPhone retina-4.7 inch(iPhone 6)
}
else if([UIScreen mainScreen].bounds.size.height == 568){
// iPhone retina-4 inch(iPhone 5 or 5s)
}
else{
// iPhone retina-3.5 inch(iPhone 4s)
}
}
else if ([[UIScreen mainScreen] scale] == 3.0)
{
if([UIScreen mainScreen].bounds.size.height == 736.0){
//iPhone retina-5.5 inch screen(iPhone 6 plus)
}
}
}
Reference from Here
write this code in .pch file
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
and wwhenever you need in any controller you simply check the condition.
Below are methods that do what you asked. One major thing to look out for is in iOS7 and below, when you check [[UIScreen mainScreen] bounds] for the screen bounds, it always lists the width and height as the same no matter what orientation the phone is in. So if it's an iPhone5 in landscape mode, it will still list the width as 320 and height as 568. In iOS8, this changed, now if that same iPhone5 is in landscape, it will list the width as 568 and the height as 320. Below are methods which account for this:
+ (BOOL) deviceHasFourInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:568.0];
}
+ (BOOL) deviceHasFourPointSevenInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:2.0 height:667.0];
}
+ (BOOL) deviceHasFivePointFiveInchScreen
{
return [DeviceType deviceHasScreenWithIdiom:UIUserInterfaceIdiomPhone scale:3.0 height:736.0];
}
+ (BOOL) deviceHasScreenWithIdiom:(UIUserInterfaceIdiom)userInterfaceIdiom scale:(CGFloat)scale height:(CGFloat)height
{
CGRect mainScreenBounds = [[UIScreen mainScreen] bounds];
CGFloat mainScreenHeight;
if ([OperatingSystemVersion operatingSystemVersionLessThan:#"8.0"])
{
mainScreenHeight = mainScreenBounds.size.height;
}
else
{
mainScreenHeight = (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ? mainScreenBounds.size.width : mainScreenBounds.size.height;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == userInterfaceIdiom && [[UIScreen mainScreen] scale] == scale && mainScreenHeight == height)
{
return YES;
}
else
{
return NO;
}
}
Also here are the accompanying OperatingSystem class methods:
+ (NSString *) currentOperatingSystemVersion
{
return [[UIDevice currentDevice] systemVersion];
}
+ (BOOL) operatingSystemVersionLessThanOrEqualTo:(NSString *) operatingSystemVersionToCompare
{
return ([[self currentOperatingSystemVersion] compare: operatingSystemVersionToCompare options:NSNumericSearch] != NSOrderedDescending);
}
Put below lines in prefix.pch
#define iPhoneVersion ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : ([[UIScreen mainScreen] bounds].size.height == 667 ? 6 : ([[UIScreen mainScreen] bounds].size.height == 736 ? 61 : 999))))
Now in programming you can say...
if (iPhoneVersion==4) {
NSLog("This is 3.5 inch iPhone - iPhone 4s or below");
} else if (iPhoneVersion==5) {
NSLog("This is 4 inch iPhone - iPhone 5 family");
} else if (iPhoneVersion==6) {
NSLog("This is 4.7 inch iPhone - iPhone 6");
} else if (iPhoneVersion==61) {
NSLog("This is 5.5 inch iPhone - iPhone 6 Plus.. The BIGGER");
} else {
NSLog("This is iPad");
}
However, I would say use one storyboard for all iPhones by learning autolayout.

Detect iPhone different resolution in iOS

I am using cocos2d in iOS .
I am having a problem detecting the iphone5 resolution(1136*640).
I am using code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
When we run app on iPhone simulator (4 inch), it gives screenWidth and screnHeight-320,480 respectively.
Can anyone tell me the solution for this?
Write this code in .m file.
#define WIDESCREEN ( fabs( ( double )[ [UIScreen mainScreen ]bounds ].size.height-( double )568 ) < DBL_EPSILON )
Now you can know the screen size like this.
if(WIDESCREEN)
{
NSLog(#"iPhone5 is here");
}
else
{
NSLog(#"iPhone4 is here");
}
you can add checks in your code for iPhone 4inch screens and 3.5 inch screens
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
// set your frames for classic iPhone here
}
if(result.height == 568)
{
// iPhone 5, iPhone 5S, iPhone 5C
// set your frames for 4 inch iPhone here
}
iPhone 5 resolution is 1136*640 only. If you wanna check just Log below Code
the resolution it will give exactly (1136*640)
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
NSLog(#"Screen SIze %#",NSStringFromCGSize (screenSize));
#define IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.width >= 568 || [[UIScreen mainScreen] bounds].size.height >= 568)?YES:NO
#define IS_IPHONE4 ([[UIScreen mainScreen] bounds].size.width >= 480 || [[UIScreen mainScreen] bounds].size.height >= 480)?YES:NO
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if(IS_IPHONE4)
{
// iPhone 4
}
if(IS_IPHONE5)
{
// iPhone 5
}
}

Find device type iPhone 4 or iPhone5 not working [duplicate]

This question already has answers here:
How to detect iPhone 5 (widescreen devices)?
(24 answers)
Closed 9 years ago.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGSize result = [[UIScreen mainScreen] bounds].size;
//NSLog(#"phonesize%#",result);
if(result.height > 500){
NSLog(#"iPhone 5");
}
else{
NSLog(#"iPhone 4");
}
}
My Mac OS is 10.7.2 and Xcode 4.5.2.I used above code to find device is iPhone 4 or iPhone 5
It is giving iPhone4 when i uses iPhone5.I did set launch image and icon image. Is there anything else i missed out?
Make sure you have a PNG file named Default-568h#2x.png in your project.
You need to scale the result you got as per the screen's scale scale and then check ...
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)]) {
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960) {
NSLog(#"iPhone 4");
}
if(result.height == 1136) {
NSLog(#"iPhone 5");
}
}
else{
NSLog(#"Standard Resolution");
}
}
Try this code instead its tested.
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
{
//device is iPhone
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
//device is iphone 5
//add storyboard/Xib that have large screen (320*568)
} else {
//device is iphone 4
//add story board/Xib that have small screen (320*480)
}
}
else
{
//device is iPad
}
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//iphone 5
else
{
//iphone 4
}
}
else
{
//iPad
}
Try this one might be helpful to you...

Button with different width on iPhone 5

I want to increase the size of a custom button if the user is using an iPhone 5.
This is what I have in my .m file
//.m File
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
int varWidth = 228;
}
if(result.height == 568)
{
int varWidth = 272;
}
}
....
[newButton setFrame:CGRectMake(8.0, 40.0, 228, 80.0)];
But I want something like this:
[newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];
You are using varWidth out of it's scope.
int varWidth;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
varWidth = 228;
}
if(result.height == 568)
{
varWidth = 272;
}
}
....
[newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[newButton setFrame:CGRectMake(8.0, 40.0, 228.0, 80.0)];
}
if(result.height == 568)
{
[newButton setFrame:CGRectMake(8.0, 40.0, 272, 80.0)];
}
}
Why not do this way?
Another suggestion:
Use #define.
For example:
#define iPhone5width 272
#define iPhone4width 228
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[newButton setFrame:CGRectMake(8.0, 40.0, iPhone4width, 80.0)];
}
if(result.height == 568)
{
[newButton setFrame:CGRectMake(8.0, 40.0, iPhone5width, 80.0)];
}
}
The Best and Short way, For check device is iPhone 5 or iPhone5 < (Less Then).
For get this you need to write following code in your .pch file of project.
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
This Like check device, is it iPhone5 or not.
And you need to write only one condition for manage it
if( IS_IPHONE_5 )
// set or put code related to iPhone 5.
else
// set or put code related to less then iPhone 5.

Set UIView height depending on screen size

I've a UIView under a table.
I've got to set UIView height for iPhone 4S or 5.
I tried using
- (void) regolaViewPeriPhone {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
CGRect frame = CGRectMake(0, 210, 320, 245);
_pickerContainerView.frame = frame;
}
if(result.height == 568)
{
CGRect frame = CGRectMake(0, 210, 320, 290);
_pickerContainerView.frame = frame;
}
}
}
but it doesn't work, what's right method? Thank you!
[ [ UIScreen mainScreen ] applicationFrame ]
This is the way to "Set UIView height depending on screen size"
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//Do u r stuff here for Iphone 5
}
else if(screenBounds.size.height==480)
{
//Do u r stuff here for Iphone 4s
}
//if my answer is correct then vote up for me...

Resources