2016-09-26 93 views
0

基於互聯網連接,我必須刪除或添加子視圖超級視圖。如何在運行時從超級視圖中刪除視圖?

我可以在運行時添加子視圖。但不能從子視圖中刪除。

我試過這樣

if ([statusString isEqualToString:@"Access Not Available"]){ 
    view = [[UIView alloc]initWithFrame:CGRectMake(0, navigationView.frame.size.height, self.view.frame.size.width, 50)]; 
    [self.view addSubview:view]; 
    view.backgroundColor = [UIColor lightGrayColor]; 
}else{ 
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ { 

     [view removeFromSuperview]; 
    }]; 
} 

但它不是從上海華刪除。

我該怎麼做?

+1

這將工作,除非'view'爲零。 – Avi

+0

你能比「它不工作」更具體嗎?結果是什麼? – Arc676

+0

@ Arc676看到我編輯的問題 – Himanth

回答

0
///in view did load 
view_NoConnectn = [[UIView alloc]init]; 
[view_NoConnectn setBackgroundColor:[UIColor whiteColor]]; 
[view_NoConnectn setFrame:CGRectMake(0, frameHeight, frameWidth, 35)]; 

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow]; 
bool isFound=false; 
for(UIView *child in [mainWindow subviews]) 
{ 
    if([child tag]==007) 
     isFound=true; 
} 
if(!isFound) 
{ 
    [mainWindow addSubview: btn_setting]; 
} 

[self.navigationController.view addSubview:view_NoConnectn]; 



////// whereever required 
    if (show){ 
    if (self.navigationController.view.frame.size.height == frameHeight) { 
     [UIView animateWithDuration:1.0 animations:^{ 
      [self.navigationController.view setFrame:CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y, self.navigationController.view.frame.size.width, frameHeight - 35)]; 
      [view_NoConnectn setFrame:CGRectMake(0, frameHeight-35, frameWidth, 35)]; 
      [self.view layoutIfNeeded]; 
     }]; 

    } 

} 
else 
{ 
    if (self.navigationController.view.frame.size.height != frameHeight) { 
     [UIView animateWithDuration:1.0 animations:^{ 
      [UIView animateWithDuration:0.8 animations:^{ 
       [self.navigationController.view setFrame:CGRectMake(self.navigationController.view.frame.origin.x, self.navigationController.view.frame.origin.y, self.navigationController.view.frame.size.width, frameHeight)]; 
       [view_NoConnectn setFrame:CGRectMake(0, frameHeight, frameWidth, 35)]; 
       [self.view layoutIfNeeded]; 
      }]; 
     } completion:^(BOOL finished) { 
      [view_NoConnectn removeFromSuperview]; 
     }]; 
    } 

} 
相關問題