2012-07-26 56 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