2012-03-08 55 views
0

默认的背景色为白色,我想将其更改为图像如何设置视图的背景图像?

我已经试过这样:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; 

但研究背景变成黑色,图像不是在后台。我不知道为什么....

顺便说一下,如果我在一个视图中有超过1个图像,如何设置它们的显示优先级?

+1

应该工作,你确定你输入你的形象? – Simon 2012-03-08 21:18:35

+0

它现在的作品,但我真的不知道为什么......我什么都没变...... – iPhrog 2012-03-08 22:04:56

回答

2
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageone"]]; 
backgroundView.frame = self.view.bounds; 
[[self view] addSubview:backgroundView]; 

尝试了这一点,这是呼唤self.view.bounds的框架,而不是self.view.frame帧。我只是测试它,它完美的作品。

编辑:如果你正在寻找的是发送图像到堆栈的后面,你可以执行以下操作并选择发回。

enter image description here

+1

这对我有用。确保你有正确导入的图像。 – 2012-03-08 21:50:35

+0

我从来没有在视图中看到过这种类型的图像...有时它是白色的,有时是黑色的......也许在其他地方有一些问题。只有当我在我的.xib文件中放置一个“图像视图”时,图像才会显示出来,但是它在前面而不是后面...... – iPhrog 2012-03-08 21:59:24

+0

它现在可以工作,但我真的不知道为什么......我没有改变任何东西。 – iPhrog 2012-03-08 22:05:21

0

您应该使用UIImageView,其框架是uiview的框架。

+0

感谢您的回答。但我仍然不知道该怎么办... – iPhrog 2012-03-08 21:19:12

1

代码:

UIImageView *myImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]]autorelease]; 
myImage.frame = self.view.frame; 
[self.view addSubview:myImage]; 
+0

我已经试过你的代码,这次,背景变成了白色...... – iPhrog 2012-03-08 21:24:48

+0

我已经忘记了[self.view addSubview:myImage];尝试与;) – Victor 2012-03-08 21:25:22

+0

你如此善良,但背景保持白色... T.T – iPhrog 2012-03-08 21:38:50