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.
Related
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
}
}
I have a universial app .I added Launch images to project .
I want to wait splash screen until download json from webservice. my app it's waiting until download json. but launch image choosing is not correct or position.I wrote this following code.for example: my launch image correct on iphone 5 . but not on iphone 3gs.
How can I do that easily? sorry my English . Thank you.
CGRect screenRect = [[UIScreen mainScreen] bounds];
float screenWidth = screenRect.size.width;
float screenHeight = screenRect.size.height;
splashView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
NSLog(#"height %f version %f",[[UIScreen mainScreen] bounds].size.height,sysVer);
if (sysVer <7.0)
{
// iOS-6.01 and prior code
if (screenHeight==480)
{
splashView.image=[UIImage imageNamed:#"Default"];//iPhone
}
else if (screenHeight == 568.0f)
{
splashView.image=[UIImage imageNamed:#"Default-568h#2x.png"];
}
}
else // for ios7
{
if (!screenHeight == 568.0f)
{
splashView.image=[UIImage imageNamed:#"Default"];//iPhone
}
else
{
splashView.image=[UIImage imageNamed:#"Default-568h#2x.png"];
}
}
}
else
{
if (sysVer <=6.1)
{
splashView.image=[UIImage imageNamed:#"Default-Portrait"];// iPads
}
else
{
splashView.image=[UIImage imageNamed:#"Default-7681024"];// iPads
}
}
[self.view addSubview:splashView];
log is on iphone 3gs
height 480.000000 version 6.100000
I am new to iOS programming. I made an application for iPhone retina 4-inch. Now I want to customise my code in order for it to run on an iPhone retina-3.5 inch and iPhone 5.
I tried to create an if query for self.view.frame.size.width and self.view.frame.size.height, but I get a "Not assignable" error.
Can anyone tell me how to specify conditions for setting up the workspace nicely in all devices? If possible please give me the exact screen-size of all that devices.
You can use this:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
References:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html
You look at the device's screen size (in points) and from that surmise if it's an iPad or iPhone etc., and then use hard-coded values for the screen sizes.
Here's some code to get the screen size:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Be aware that width and height might be swapped, depending on device orientation.
to compare screen size include this macro and use it any where necessary
#define isPhone5 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
if you want to compare screen sizes only then reduce the above macro to
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
For example in your .m file
#import "ViewController.h"
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE //hear u put the macro and use it anywhere in this file
#interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if(isiPhone5)
{
NSLog(#"I am 4inch screen");
}
else
{
NSLog(#"I am 3.5inch screen");
}
// Do any additional setup after loading the view, typically from a nib.
}
Try this In your .M file:
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
//Write here code for iPad (Landscape mode)
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
//Write code here for iPhone 5 (Landscape mode)
}
else
{
//Write code here for iPhone(3.5 inch) (Landscape mode)
}
}
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
}
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
if (ITS_IPHONE5)
{
}
else
{
}
Use This Code in project-Prefix.pch file in supporting file folder.
#define IS_IPHONE_SIMULATOR ([[[UIDevice currentDevice]model]
isEqualToString : #"iPhone Simulator"])
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString : #"iPhone"])
#define IS_IPOD_TOUCH ([[[UIDevice currentDevice]model] isEqualToString : #"iPod Touch"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define ITS_IPHONE5 ( IS_HEIGHT_GTE_568 )
Copy and Past This Code After That in your .m File past the first code i give you...
Dont need to write size on viewdidload write only in these cots
if (IS_IPAD)
{
}
else if(IS_IPHONE)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
}
else
{
}
}
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;
}
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...