2012-07-26 54 views
0

向我的UIView(View_0)添加背景(图像)后,我希望实现一个按钮来删除相同的背景。如何重置UIView的背景

我该怎么办?我试图设置一个不同的图像作为View_0的背景,但这不起作用(我怀疑这个背景设置在“图像”后面)。

我不想使用

[View_0 removeFromSuperview]; 

,因为我需要View_0仍然在那里为其他目的....

//Create an ImageView 
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;   

//Add the ImageView as the background of the View_0 
[View_0 addSubview:image]; 
//Move custom image behind View 
[View_0 sendSubviewToBack: image]; 

//Failed attempt to remove the background.... 
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"defaultImage.PNG"]];  

回答

1

您已将UIImageView添加到视图而不是视图的背景。那么,什么是情况是,当u实现

View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"defaultImage.PNG"]]; 

的视图的背景被设定,但现在的UIImageView是在视图的顶部,这就是为什么没有被显示的视图的背景。

相反,如果您只是想添加背景,您可以将其更改为UIButton的操作。

此外,如果您使用[View_0 removeFromSuperview];您试图删除不正确的视图。

1
//Create an ImageView 
     UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];; 
     image.tag = 1234;   

     //Add the ImageView as the background of the View_0 
     [View_0 addSubview:image]; 
     //Move custom image behind View 
     [View_0 sendSubviewToBack: image]; 

     UIImageView *imgView = [self.view viewWithTag:1234]; 
     [img setImage:nil]; 

这应该工作,我认为

+0

它的确如此!非常感谢! – jeddi 2012-07-26 10:58:46

+0

这是因为您完全删除图像。只需保留它们并设置imageview.hidden = YES当你不需要一个。 – IronManGill 2012-07-26 15:06:30