将启动图像用作ViewController背景图像

问题描述:

我以适当的分辨率创建了3个启动图像(默认默认@ 2x Default-568 @ 2x)。我想使用这些图像作为我的初始视图控制器的背景图像。将启动图像用作ViewController背景图像

但是,当我设计我的启动图像时,我考虑了状态栏并将该区域留为空白。当我尝试使用以下代码将View Controller的背景图像设置为启动图像时,我的启动图像从状态栏的底部开始,空白区域可见。本质上,我的640x1136发射图像被挤压到一个640x1096的空间。什么是正确的方式来做我想做的事情?

UIImage *backgroundImage = [[UIImage alloc]init]; 
if ([[UIScreen mainScreen] bounds].size.height == 568) { 
    backgroundImage = [UIImage imageNamed:@"[email protected]"]; 
} 
else{ 
    backgroundImage = [UIImage imageNamed:@"Default"]; 
} 

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

更新:

我代替:

[backgroundImageView setFrame:[[self view] bounds]]; 

有了:

[backgroundImageView setFrame:CGRectMake(0 ,-20,self.view.frame.size.width , self.view.frame.size.height+20)]; 

而且它似乎是表现我现在想要的方式。

+0

从图像中删除区域空白.. – Ayaz 2013-03-06 04:26:32

+0

等待,您的图像文件中有空白区域?像像素的一部分? – WolfLink 2013-03-06 04:29:15

+0

是的前20/40px是黑色的。 如果我从图像中删除空白区域,当应用程序启动时,状态栏会覆盖图像的一部分。 – salfasano 2013-03-06 04:38:43

试试这个:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]]; 

也尽量不要设置编程backgroundImageView的框架。当您致电initWithImage:时,它应该已经是它的大小。

+0

当我尝试这个时,我的图片在截断时并未缩小。 – salfasano 2013-03-06 04:27:15

+0

它仍然在状态栏下方留下空隙吗? – WolfLink 2013-03-06 04:29:55

+0

是的...... 字符数限制 – salfasano 2013-03-06 04:37:15

利用未包含空白区域的未经编辑的图像。这将工作正常。

请使用以下代码替换此[backgroundImageView setFrame:[[self view] bounds]];

int statusBarHeight = statusBar.frame.size.height; 

    backgroundImageView.frame = CGRectMake(0 ,statusBarHeight,self.view.frame.size.width , self.view.frame.size.height-statusBarHeight); 
+0

我得到了类似于此的工作: [backgroundImageView setFrame:CGRectMake(0, -20,self.view.frame.size.width,self.view.frame.size.height + 20)]; – salfasano 2013-03-06 04:57:08

+0

我想你是用空白区域编辑过的图像啊?上面的代码对于没有空白区域的图像来说工作得很好。 – 2013-03-06 05:16:15