Search code examples
iphoneobjective-cuitableviewframecgrectmake

Distinguish between iPhone 4 and 5 to set height of UITableView


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

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.