尝试将UIImageView设置为视图

时间:2012-07-04 23:31:32

标签: ios uiview

我的搜索视图控制器searchVC有一个UIImageView* backgroundImage
我想将backgroundImage设置为我视图的图像 为什么这段代码不起作用?

[searchVC.backgroundImage initWithImage:[UIImage imageNamed:@"someimage.png"]];
[searchVC.view addSubview:searchVC.backgroundImage];

3 个答案:

答案 0 :(得分:2)

我认为应该是:

//If backgroundImage is not created yet, create it.
searchVC.backgroundImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someimage.png"]] autorelease];

// If it is already created then you should just set the image
searchVC.backgroundImage.image = [UIImage imageNamed:@"someimage.png"];

[searchVC.view addSubview:searchVC.backgroundImage];

答案 1 :(得分:0)

您需要searchVC.backgroundImage一个frameframe告诉视图在哪里以及它有多大。

由于您希望它充当背景图像,请尝试:

[searchVC.backgroundImage setFrame:CGRectMake(0, 0, searchVC.view.frame.size.width, searchVC.view.frame.size.height];

如果searchVC.view有其他子视图(我想象的那样),请确保在其他任何内容之前添加searchVC.backgroundImage作为子视图,因此它会转到视图的最后面。

答案 2 :(得分:0)

您可以使用以下代码:

searchVC.backgroundImage.image = [UIImage imageNamed:@"someimage.png"];

[searchVC.view addSubview:searchVC.backgroundImage];