0

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);
    }
}
user3298017
  • 31
  • 1
  • 6
  • 2
    why do you do this? use autolayout – meda Mar 02 '14 at 17:08
  • You are referencing `myTableView` and `self.myTableView`. Are you absolutely sure these references point to the same object? – bneely Mar 02 '14 at 17:11
  • can I use autolayout when the keyboard covers the tableView, because this is the reason why I want to do this? – user3298017 Mar 02 '14 at 18:05
  • @bneely yes I'm sorry, I forgot to change this again, because I tried it without the self. – user3298017 Mar 02 '14 at 18:06
  • @user3298017If you want to change the layout based on whether the keyboard is visible, you can respond to notifications to do this. Here is another StackOverflow question addressing this: http://stackoverflow.com/questions/1490573/how-to-programatically-check-whether-a-keyboard-is-present-in-iphone-app – bneely Mar 02 '14 at 19:47
  • @bneely the keyboard is always visible, how can I achieve it? I guess I'm on the wrong way with my solution – user3298017 Mar 03 '14 at 08:30

2 Answers2

0

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.

bneely
  • 9,083
  • 4
  • 38
  • 46
0

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.

  • so it returns '5' for iPhone 5, '3' for iPhone 4 and lower? And what do you mean with '4' for all iPads and retina iPhones? because the iPhone 5 is retina isn't it? – user3298017 Mar 03 '14 at 11:28
  • no..it will return 5 for (iPhone5S, iPhone5, iPhone4S, iPhone4, iPod5). and return 4 for all Retina iPads..and return 3 for All Non Retina iPhones and iPods. –  Mar 03 '14 at 11:58