检测iPhone5、5s,6和6 plus [英] Detect iPhone5, 5s, 6, and 6 plus

查看:97
本文介绍了检测iPhone5、5s,6和6 plus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone show me how to update the below code to work with iPhone 5, 5s, 6, and 6 +? When I test the simulator for the aforementioned devices, no image is set as the background...

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];

推荐答案

#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语句中进行比较.

Then compare in your if statement.

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中有更简单,更有效的方法来解决此问题.

I don't see why you're doing it this way though.... Is it because your image is becoming distorted based on different devices? Because there are easier and more efficient ways to solve this in IB.

这篇关于检测iPhone5、5s,6和6 plus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆