finding difference iphone 4s and 5 working wrongly - iphone

configh.h
#define IS_IPHONE5 [[UIScreen mainScreen] bounds].size.height == 568
homeViewController.m
- ( void ) phoneType{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 568){
NSLog(#"iPhone 5");
homeImg.image = [UIImage imageNamed:#"Background-Home.fw.png"];
loginImg.image=[UIImage imageNamed:#"Background.fw.png"];
}
else{
NSLog(#"iPhone 4S");
homeImg.image = [UIImage imageNamed:#"Background-Home.png"];
loginImg.image=[UIImage imageNamed:#"Background.png"];
}
}
This is the way iphone 4s and 5 i find difference.When i execute this code on iphone 5 log iphone 4s printed.how do i change my code to get execute correctly

You can try this for the same:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
}
if(result.height == 568)
{
// iPhone 5
}
}

This only works if you have at least added a Default-568h#2x.png launch image to your application. Else this will always return false. (Because the screen will be letterboxed if you don't have the launch image)
Also you might want to change the iPhone 5 check to
bool isIphone5 = (([[UIDevice currentDevice] userInterfaceIdiom]
== UIUserInterfaceIdiomPhone) && (([UIScreen mainScreen].bounds.size.height *
[[UIScreen mainScreen] scale]) >= 1136));
Because of floating point issues with == and !=

The ultimate rule of math in C: never use == and != with floating-point numbers. They don't work as you would expect it. Instead of checking it to be a specific value, check for being less or more than an intermediate value:
IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height > 500)
Also, it's advisable to always parenthesize your macros.

Related

Restrict Interface Orientation in iOS 7 and iOS 8

I am trying to implement something where I stuck at one point.
I want some orientation restrictions as follows:
For iPhone 4, 4S, 5, 5S, & 6 - Portrait Only
For iPhone 6 Plus - Both Portrait & Landscape
For iPad - Both Portrait & Landscape
I have tried all the combination of coding. Finally I got solution for iOS 8
**(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix
{**
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)])
{
if(IS_IPHONE_4_AND_OLDER){
printf("Device Type : iPhone 4,4s ");
return UIInterfaceOrientationMaskPortrait;
}
else if(IS_IPHONE_5){
printf("Device Type : iPhone 5,5S/iPod 5 ");
return UIInterfaceOrientationMaskPortrait;
}
else if(IS_IPHONE_6){
printf("Device Type : iPhone 6 ");
return UIInterfaceOrientationMaskPortrait;
}
else if(IS_IPHONE_6P){
printf("Device Type : iPhone 6+ ");
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
}
return UIInterfaceOrientationMaskPortrait;
}
else{
printf("Device Type : iPad");
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
}
By using above code I manage for iOS 8 But it does not work in iOS 7 or less
Please help me to solve this ...
Thank you
Simple but it work very fine. IOS 7.1 and 8
AppDelegate.h
#property () BOOL restrictRotation;
AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
ViewController
At top: To Detect Device Type
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f)
#define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f)
#define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f)
#define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f)
#define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: #selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 )
#define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: #selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 )
#define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: #selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 )
#define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: #selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 )
Then Add Function Below:
-(void) restrictRotation:(BOOL) restriction{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;}
viewDidLoad
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: #selector(scale)])
{
if(IS_IPHONE_6P){
printf("Device Type : iPhone 6+ ");
[self restrictRotation:NO];
}
else{
printf("Device Type : iPhone 6 ");
[self restrictRotation:YES];
}
}
}
else{
printf("Device Type : iPad");
[self restrictRotation:NO];
}

Display an App correctly on 3.5" and 4" screen

I almost finished to develop my App. Now I am trying to make it look nice in both iPhone 4 and 5.
I am trying to get the height of my scren but the following method doesn't work.
Any idea?
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
if (screenSize.height == 1136) {
NSLog(#"iphone 5");
}else if(screenSize.height == 960){
NSLog(#"iphone 4 retina");
}else{
NSLog(#"iphone 4 non retina");
}
this is the complete viewWillAppear method:
-(void)viewWillAppear:(BOOL)animated
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
NSLog(#"%#",NSStringFromCGSize(screenSize));
if (screenSize.height == 568.0f) {
NSLog(#"iphone 5");
}else if(screenSize.height == 480.0f){
NSLog(#"iphone 4 retina");
}else{
NSLog(#"iphone 4 non retina");
}
[firstStepsButton.titleLabel setHidden:YES];
[workingButton.titleLabel setHidden:YES];
[accommodationButton.titleLabel setHidden:YES];
[universityButton.titleLabel setHidden:YES];
[meetMeButton.titleLabel setHidden:YES];
[improveYourselfButton.titleLabel setHidden:YES];
self.navigationController.navigationBarHidden = TRUE;
// self.view.backgroundColor = [UIColor colorWithRed:2/255.0f green:42/255.0f blue:97/255.0f alpha:1.0f];
[super viewWillAppear:animated];
}
It wouldn't because the screen's coordinate system is in points, not pixels. Try it like this:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
NSLog(#"%#",NSStringFromCGSize(screenSize));
if (screenSize.height == 568.0f) {
NSLog(#"iphone 5");
}else if(screenSize.height == 480.0f){
NSLog(#"iphone 4 retina");
}else{
NSLog(#"iphone 4 non retina");
}
You should also consider using UIDevice's userInterfaceIdiom to check whether the device is iPhone/iPad, and UIScreen's scale to determine whether the device is retina or not.
Consider downloading Xcode 5 DP where they fixed management of Autolayout in Interface Builder. It is now really easy to set and manage constraints. Try it out.

Imageview not display Proper For Iphone 5 Retina

I am developing one app in that app display 20 array Images to Image view Scroll horizontally.
give scroll view to that imageview like this
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
for (m = 0; m <=19; m++)
{
rnd = arc4random_uniform(arr.count);
NSString *imageName=[FrontsCards objectAtIndex:rnd];
fullImageName=[NSString stringWithFormat:#"%#",imageName];
int padding=0;
CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[ImgView setImage:[UIImage imageNamed:fullImageName]];
[scrollView addSubview:ImgView];
}
this imageview not display images Properly for iphone 5 simulator. below black edge add.
how may i solve that issue.how to remove that black edge.how to fit that image to Iphone 5 simulator.
how may manage my ImageView to Iphone 4 or Iphone 5.
Iphone 5 size is 568 and Iphone 4 size is 480.
scrollView=[[UIScrollView alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
// For Iphone5
scrollView.frame = CGRectMake(0, 0, 320, 568)
}
else if ([[UIScreen mainScreen] bounds].size.height == 480)
{
//For Iphone 4 or others
scrollView.frame = CGCGRectMake(0, 0, 320, 480)
}
this will really helpful to you.
Iphone 5 screen size is 568 and you have had codded height to 480. So it is not working for iphone5.
DO following
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
Depending on devise scroll view height will change.
Try This
scrollView=[[UIScrollView alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) scrollView.frame = CGRectMake(0, 0, 320, 568)
else if ([[UIScreen mainScreen] bounds].size.height == 480) scrollView.frame = CGRectMake(0, 0, 320, 480)
So, it will help you to fill scrollview in iPhone-5 screen too.
Hopefully, it'll work for you.
Thanks.
scrollView=[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
use this to make the UIScrollView dynamic
Use macro to check whether it is iPhone 5 or not.
#define IS_IPHONE ( [[[UIDevice currentDevice] model] isEqualToString:#"iPhone"] | [[[UIDevice currentDevice] model] isEqualToString:#"iPhone Simulator"])
#define IS_HEIGHT_GTE_568 [[UIScreen mainScreen ] bounds].size.height >= 568.0f
#define IS_IPHONE_5 ( IS_IPHONE && IS_HEIGHT_GTE_568 )
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, IS_IPHONE_5?568:480)];
You need to create design for iPhone 4 and iPhone 5.
Create a class which make decision which iPhone is connected and
make function on it which accept CGRect and return required size of CGRect
you can get device size by
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height < 500)
{
currentDeviceHeight=460;
//You can use use iPhone 4 image
}
else
{
currentDeviceHeight=548;
// here you can use iPhone 5 image
}
}
else
{
// iPad
}

Issues with migrating my app to iphone5 resolution

I am trying to migrate my app for iphone 5. I have already seen the other questions in stackoverflow and still facing the some problems in correctly showing it.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
_backgroundimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,500)];
[_backgroundimageView setImage:[UIImage imageNamed:#"image1-568h#2x.png"]];
} else {
// code for 3.5-inch screen
[_backgroundimageView setImage:[UIImage imageNamed:#"image1.png"]];
}
The size I have set for backgroundimageView is 320 x370 in the size inspector and the image size of image1-568h#2x.png has a size of 640x 1136 and of image1.png is 640x733.So, for 3.5 inch screen, it should show normally and for 4-inch screen, it should resize accordingly.
But the problem is that for 4-inch screen, it doesn't resize and ends up showing me the same as 3inch screen with some white border to cover the remaining area.
Need some guidance to solve it. Thanks.. Can point out the mistake i am doing...
Why you are again allocating backgroundimageView for iPhone 5 resolutions. Just use the same IBOutlet of backgroundimageView and modify the frame and image of it.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
_backgroundimageView.frame = CGRectMake(0,0,320,460);
[_backgroundimageView setImage:[UIImage imageNamed:#"image1-568h#2x.png"]];
} else {
// code for 3.5-inch screen
_backgroundimageView.frame =CGRectMake(0,0,320,370);
[_backgroundimageView setImage:[UIImage imageNamed:#"image1.png"]];
}
Default image name must be "Default-568h#2x.png" (not image1-568h#2x.png) try renaming the default image name and check again, you will get right screen size.
Also you can check iphone5 by macros.
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
I think content mode of your imageview is center so try this for your imageview
[_backgroundimageView setContentMode:UIViewContentModeScaleAspectFill]

Iphone: Get current view dimensions or screen dimensions

Hello i want to get the width and height of my main view. I want the correct value in landscape or portrait mode. I've tried the following:
NSLog(#"aaa %f", [UIScreen mainScreen].applicationFrame.size.width);
NSLog(#"zzz %f", self.view.frame.size.width);
These give 300 in landscape and 320 in portrait mode, yes it is larger in portrait mode. So.. my view takes up the whole screen (- status bar) so I expect 480 in landscape mode and 320 in portrait mode. What happened to the rest of the pixels. Do I have to hardcode these values? Thanks.
Do either of the following lines work for you?
[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
I suppose you can subtract the height of the status bar? Maybe i'm not understanding your question
Try this :
UIWindow* window = [UIApplication sharedApplication].keyWindow;
NSLog(#"%f",window.frame.size.width);
Useful macros that will give you the correct size based on the device orientation:
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)