简体   繁体   English

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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