简体   繁体   中英

How to remove view from superview in run time?

Based on internet connectivity I have to remove or add subview to superview.

I can able to add subview in runtime. But not remove from subview.

I tried like this

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

But it is not removing from superview.

How can I do this?

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM