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

2 回答 2

0

假设更新后的要求是正确的,以下应该可以工作:

#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")

对于 4" 屏幕 iPhone 和 iPod touch,这将返回 @"5"。对于所有 iPad 和视网膜 iPhone 和 iPod touch,这将返回 @"4"。对于非视网膜 iPhone 和 iPod touch,它将返回 @"3" .

于 2014-03-03T09:36:56.997 回答
0

我同意您应该改用自动布局的建议。但是以您的硬编码帧大小为例,您的代码可以更简单地编写:

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

当然,这是一个完全基于现有屏幕高度 480 和 568 的假设(这些以点为单位,而不是像素)。因此,不能保证通过这种方式简化代码,任何未来的屏幕尺寸都会为您提供正确的行为。您可以完成的最佳行为是使用自动布局来控制 tableView 的位置和大小。

于 2014-03-02T17:15:24.257 回答