检测iPhone5、5s、6、6加
Detect iPhone5, 5s, 6, and 6 plus
有人可以告诉我如何更新以下代码以使用 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];
#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中有更简单有效的方法来解决这个问题。
有人可以告诉我如何更新以下代码以使用 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];
#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中有更简单有效的方法来解决这个问题。