Question

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

}

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top