简体   繁体   English

如何设置视图的背景图像?

[英]How to set background image for a View?

the default background color is white and I want to change it to an image 默认的背景颜色是白色,我想将其更改为图像

I have tried this: 我已经试过了:

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

but the background turn black, the image is not at the background. 但背景变成黑色,则图像不在背景中。 I don't know why. 我不知道为什么

by the way if I have more than 1 image in one view, how to set their priority of display? 顺便说一句,如果我在一个视图中有1个以上的图像,如何设置其显示优先级?

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

Try this out, this is calling self.view.bounds for the frame instead of self.view.frame for frame. 尝试了这一点,这是呼唤self.view.bounds的框架,而不是self.view.frame帧。 I just tested it and it works perfectly. 我刚刚对其进行了测试,并且效果很好。

EDIT: If all you're looking for is to send the image to the back of the stack, you can do the following and select send to back. 编辑:如果您要查找的只是将图像发送到堆栈的后面,则可以执行以下操作,然后选择“发送到后面”。

在此处输入图片说明

Code for you : 代码为您:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM