我正在尝试覆盖不同的背景图像以在我的应用程序上运行。我用过这个问题的代码; set background images for Iphone 3g, Iphone 4s and Iphone 5
谢天谢地,代码很简单。我还在.m文件中设置这些以防万一。
.h
@property (retain strong) UIImage* Image
@property (retain, strong) UIImageView.
的.m
@interface ViewController ()
@end
@implementation ViewController
//SYNTHESIZE JUST IN CASE
@synthesize backgroundImageView=backgroundImageView;
@synthesize Image=Image;
- (void)viewDidLoad
{
//SETS A UIIMAGEVIEW TO BE SET TO A CUSTOM UIIMAGE (JPG)
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 == 568) {
Image = [UIImage imageNamed:@"11@2x.jpg"];
}
else
//THIS ELSE STATEMENT IS THE IMAGE THAT ACTUALLY RASTERIZES FOR BOTH IPAD AND IPOD TEST DEVICES
{
Image = [UIImage imageNamed:@"7@2x.jpg"];
}
self.view.backgroundColor = [UIColor colorWithPatternImage:Image];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
覆盖功能并不像预期的那样有效。无论我在if语句中设置什么图像,都只运行else语句。
我的问题; if语句中的尺寸(568)是否正确光栅化iPad设备的单独图像?如果没有,请告诉我,谢谢。
其他信息:部署目标:6.1 附加信息:我在运行时收到错误(即使它仍然运行)类似于我的笔尖文件和故事板之间的差异。我不是在使用笔尖。 ?
答案 0 :(得分:1)
以下是正确的值:
if ([[UIScreen mainScreen] bounds].size.height == 480) {
// iPhone, iPod Touch
}
if ([[UIScreen mainScreen] bounds].size.height == 568) {
// iPhone 5
}
if ([[UIScreen mainScreen] bounds].size.height == 750) {
// iPhone 6
}
if ([[UIScreen mainScreen] bounds].size.height == 1024) {
// iPad
}
if ([[UIScreen mainScreen] bounds].size.height == 1242) {
// iPhone 6 Plus
}
[UIScreen mainScreen] bounds] {{0, 0}, {320, 480}}
[UIScreen mainScreen] bounds] {{0, 0}, {320, 568}}
[UIScreen mainScreen] bounds] {{0, 0}, {750, 1334}}
[UIScreen mainScreen] bounds] {{0, 0}, {768, 1024}}
[UIScreen mainScreen] bounds] {{0, 0}, {1242, 2208}}
修改:添加了iPhone 6和6 Plus按照Jules'评论维护此列表,但不建议使用此方法区分手机甚至选择正确的形象。
正确方法
使用资产目录。