简体   繁体   中英

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?

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. 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的框架。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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