I use this code in "viewDidLoad" to check if app is running on iPhone 5 or regular iPhone.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
[_backgr setImage:[UIImage imageNamed:#"usefull_i4.png"]];
[scrollView setFrame:CGRectMake(16, 69, 291, 349)];
[_backgr setFrame:CGRectMake(0, 0, 320, 480)];
}
if(result.height == 568)
{
// iPhone 5
[_backgr setImage:[UIImage imageNamed:#"usefull_i5.png"]];
[scrollView setFrame:CGRectMake(15, 111, 295, 349)];
[_backgr setFrame:CGRectMake(0, 0, 320, 568)];
}
}
It doesn't work. :( Only if I change !here from iPhone 3.5 to iPhone 4, this code work. 1
Also I use this code for another ViewController and works great.. I don't use Autolayout.
Try this macro : (in .pch file)
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
Example :
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if(IS_IPHONE_5)
{
// code
}
}
Related
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.
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
}
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.
i have a requirnmant for ipad to show contents both in landscape and portrait..so i have done the code and done the postions of controlles in each orientations.it works fine..my code is
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
_viewContainer.frame = CGRectMake(342, 1, 681, 707);
_viewtopic.frame = CGRectMake(1, 62, 343, 56);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 6, 341, 58);
_viewTableview.frame = CGRectMake(1, 118, 341, 590);
_viewFooter.frame = CGRectMake(1, 716, 1024, 48);
if (interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
_viewContainer.frame = CGRectMake(276, 1, 503, 946);
_viewtopic.frame = CGRectMake(0, 58, 275, 50);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 1, 275, 58);
_viewTableview.frame = CGRectMake(1, 109, 274, 837);
_viewFooter.frame = CGRectMake(1, 954, 767, 48);
}
return YES;
}
the above code works fine for me,i got alla the controllers in corect postions..here is my probm,,i have to set a timer to show full screen and i need to hide some controlers in the viewcontroller,,i had done the timer set to 3 second and after 3 second it hides some controlles and arrange the othe controlers in the view controller to show the ful sceen.it also works fine for me.my code fo this is
- (void)viewDidLoad
{
[super viewDidLoad];
// Define the timer object
NSTimer *timer;
// Create the timer object
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self
selector:#selector(updateorientation:) userInfo:nil repeats:NO];
}
- (void) updateorientation:(NSTimer *)incomingTimer
{
_tableTopic.hidden =YES;
_viewtopic.hidden =YES;
_viewpublicspeekingheaderleft.hidden =YES;
_viewTableview.hidden =YES;
_imgpulbicspeakingabovethetableview.hidden =YES;
_topiclabel.hidden =YES;
_lblpublicspeekingleft.hidden =YES;
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) {
NSLog(#"portrait");// only works after a rotation, not on loading app
_imgMicimage.frame = CGRectMake(69, 130, 635, 216);
_txtContentofTopic.frame = CGRectMake(69, 354, 635, 203);
_viewContainer.frame = CGRectMake(0, 0, 768, 1024);
}
UIInterfaceOrientation orientationLandscape =[[UIApplication sharedApplication] statusBarOrientation];
if (orientationLandscape == UIDeviceOrientationLandscapeLeft || orientationLandscape == UIDeviceOrientationLandscapeRight ) {
NSLog(#"landscape");
_imgMicimage.frame = CGRectMake(93, 113, 836, 239);
_txtContentofTopic.frame = CGRectMake(93, 360, 836, 203);
_viewContainer.frame = CGRectMake(0, 0, 1024, 768);
}
}
it works fine,but after the timer fires ,then i chnage the orientation of the device ,i only get the arrangmnts in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { function.i need the arrangmnts in the time function.how to solve this.
thanks in advance.
}
You should not be doing this work in -shouldAutorotateToInterfaceOrientation:. That method is simply to determine whether your UI should rotate (and will soon be superceded by a new system). You should use these methods to lay out your UI in reaction to an actual rotation:
-willRotateToInterfaceOrientation:duration:
-willAnimateRotationToInterfaceOrientation:duration:
-didRotateFromInterfaceOrientation:
Please see the UIViewController docs for more on those.
On iOS 5, -viewWillLayoutSubviews might be another good choice. (It will be called even if your controller's view isn't currently visible.)
I want to set position of UILable programatically depend on orientation. But When I get back to Portrait, it doesn't work.
Here is my code :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
lblUserName.frame = CGRectMake(20, 200, 280, 44);
lblPassword.frame = CGRectMake(20, 246, 280, 44);
}
else
{
lblUserName.frame = CGRectMake(20, 220, 280, 44);
lblPassword.frame = CGRectMake(20, 266, 280, 44);
}
}
There are 2 possibilities.Either you can set the label programatically.And check the condition for orientation in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
method.Otherwise you can use auto resizing mask.
try with this IF condition:
if ( UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
}
else {}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//write code here
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//write code here
}
Use this two methods for set your label when rotate device.