【问题标题】:How to remove view from superview in run time?如何在运行时从超级视图中删除视图?
【发布时间】:2016-09-26 10:43:37
【问题描述】:

基于互联网连接,我必须删除或添加子视图到超级视图。

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

我试过这样

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

但它并没有从超级视图中删除。

我该怎么做?

【问题讨论】:

  • 除非view 为nil,否则这将起作用。
  • 你能比“它不工作”更具体吗?结果如何?
  • @Arc676 查看我编辑的问题
  • 您的子视图是UIViewController 吗?您可能正在寻找removeFromParentView()
  • 您正在if 语句的真实块内创建视图。这意味着在 false 块中捕获的值是 nil

标签: ios objective-c view runtime


【解决方案1】:
///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];
        }];
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多