检测iPhone5,5s,6和6 plus

时间:2015-04-17 02:03:50

标签: ios objective-c iphone ipad

有人可以告诉我如何更新以下代码以使用iPhone 5,5s,6和6 +吗?当我为上述设备测试模拟器时,没有图像被设置为背景......

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:_Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
//I EXCPECTED THIS NEXT LINE OF CODE TO WORK TO SET A SEPARATE BACKGROUND FOR IPAD...
UIImage *Image = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 480) {
    // iPhone, iPod Touch
    Image = [UIImage imageNamed:@"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
    // supposably iPhone 5 but only works for 4
    Image = [UIImage imageNamed:@"image.png"];
}
if ([[UIScreen mainScreen] bounds].size.height == 1024) {
    // iPad
    Image = [UIImage imageNamed:@"image.png"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:Image];

1 个答案:

答案 0 :(得分:0)

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)

    #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
    #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
    #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
    #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))

    #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
    #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
    #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

然后在你的if语句中进行比较。

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:_Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];

UIImage *Image = [[UIImage alloc]init];

if(IS_IPHONE_5)
    {
        Image = [UIImage imageNamed:@"image.png"];
    }
    if(IS_IPHONE_6)
    {
        Image = [UIImage imageNamed:@"image.png"];
    }
    if(IS_IPHONE_6P)
    {
        Image = [UIImage imageNamed:@"image.png"];
    }

我不明白为什么你这样做会这样做......是不是因为你的形象因不同设备而变形?因为在IB中有更简单,更有效的方法来解决这个问题。