alignment for different ios device - ios

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
{
}
}

Related

Is it okay to use this code to detect the iPhone screen size?

I know it's not a great idea to alter an app based on the screen size but I am trying to modify the camera and use a custom overlay image so I need to know the screen size. There are a bunch of solutions to detecting the screen size using macros etc I am using this:
-(void)detectPhone{
CGFloat width;
width= [[UIScreen mainScreen] bounds].size.width;
if(width==320) {
CGFloat height;
height = [[UIScreen mainScreen] bounds].size.height;
if(height==480) {
NSLog(#"iPhone 4/4s");
}
else {
NSLog(#"iPhone 5");
}
}
else if (width==375) {
NSLog(#"iPhone 6");
}
else {
NSLog(#"iPhone 6+");
}
}
Is there anything wrong with using this method or any situations where it wouldn't work? Ran it through the simulator seems fine. Any pointers would be appreciated. Thanks
Its fine this way, but you are comparing floats, so I would avoid using == for that, instead use >
I use this :
#define IS_IPHONE [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone
#define PJ_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define PJ_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define PJ_SCREEN_MAX_LENGTH (MAX(PJ_SCREEN_WIDTH, PJ_SCREEN_HEIGHT))
#define PJ_SCREEN_MIN_LENGTH (MIN(PJ_SCREEN_WIDTH, PJ_SCREEN_HEIGHT))
#define IS_HEIGHT_GTE_568 PJ_SCREEN_MAX_LENGTH >= 568.0f
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:#selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
#define IOS7_DELTA_HEIGHT (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) ? 20:0)
#define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size
If you want to check device model, take a look at: Determine device (iPhone, iPod Touch) with iPhone SDK
I think you will have problems with iOS8. From iOS8
[UIScreen mainScreen].bounds
is interface-oriented (look at session "View Controller Advancements in iOS 8" of WWDC 2014), this means that in landscape mode on iOS8 height and width are reversed.
You can create a helper with a code like below:
CGRect bounds = [UIScreen mainScreen].bounds;
if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
//Your code to detect the device
...
}
You should have the screen for one time only then you can use it in your entire app (whenever you need). For this you can have a variable CGSize deviceSize in AppDelegate.h and in AppDelegate.m in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in first line of this method, you can get the device size like this,
deviceSize = [[UIScreen mainScreen] bounds].size;
and you can define a function like this,
- (CGSize) sizeOfDevice {
return deviceSize;
}
add it in AppDelegate.h like this,
- (CGSize) sizeOfDevice;
then you can use it with your delegate object.

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.

Launch image choose longer

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

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...

coding for iphone 5 screen

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;
}

Resources