Question

I want to check if the users device is an iPhone 4 or 5 and then set the height of a tableView. The xCode simulator recognizes that it is an iPhone 4 the message 'iPhone 4' is shown, but the height of the tableView stays the same. What am I doing wrong?

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        // iPhone 4

        NSLog(@"iPhone 4");
        myTableView.frame = CGRectMake(0, 44, 320, 200);
    }
    if(result.height == 568)
    {
        // iPhone 5

        self.myTableView.frame = CGRectMake(0, 44, 320, 288);
    }
}
Was it helpful?

Solution

Assuming the updated requirements are correct, the following should work:

#define iPhoneType (fabs((double)[UIScreen mainScreen].bounds.size.height - (double)568) < DBL_EPSILON) ? @"5" : ([UIScreen mainScreen].scale==2 || UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"4" : @"3")

This will return @"5" for the 4" screened iPhones and iPod touches. This will return @"4" for all iPads and retina iPhones and iPod touches. And it will return @"3" for non-retina iPhones and iPod touches.

OTHER TIPS

I agree with the suggestion that you should instead use autolayout. But to use your hard-coded frame size as an example, your code can be written more simply:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    CGSize result = [[UIScreen mainScreen].bounds;
    myTableView.frame = CGRectMake(0.0, 44.0, 320.0, 200.0+(result.height - 480.0));
}

Of course, this is an assumption based completely on existing screen heights of 480 and 568 (and these are in points, not pixels). So there's no guarantee that by simplifying your code this way, any future screen sizes would give you the correct behavior. The best behavior you can accomplish is by using autolayout to control the location and size of your tableView.

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