如何在运行时从超级视图中删除视图?

问题描述:

基于互联网连接,我必须删除或添加子视图超级视图。如何在运行时从超级视图中删除视图?

我可以在运行时添加子视图。但不能从子视图中删除。

我试过这样

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

///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]; 
     }]; 
    } 

}