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

问题描述 投票:0回答:3

默认背景颜色为白色,我想将其更改为图像

我试过这个:

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

但背景变黑,图像不在背景。我不知道为什么。

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

iphone objective-c ios view
3个回答
2
投票
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageone"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];

试试这个,这是为框架而不是self.view.bounds框架调用self.view.frame。我只是测试它,它完美地工作。

编辑:如果您要查找的只是将图像发送到堆栈的后面,您可以执行以下操作并选择发送回。


1
投票

代码给你:

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

0
投票

你应该使用UIImageView,它的框架是uiview的框架。

© www.soinside.com 2019 - 2024. All rights reserved.