无法从 self.navigationcontroller.view 中删除/隐藏最近添加的视图



我有一个导航视图控制器,里面有一个表格视图。我正在将UIView添加到self.navigationcontroller.view和自动布局的约束中。当我尝试将其从超级视图中删除或隐藏时,没有任何反应。

有什么想法吗?

如何创建视图和约束

-(void)doneWithTraining{
        //Create Backgroundview
        UIView *groupView = [[UIView alloc] init];
        [groupView setTranslatesAutoresizingMaskIntoConstraints:NO];
        groupView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        groupView.tag = 999;
        groupView.alpha = 0.0;
        [self.navigationController.view addSubview:groupView];

        [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
                                                                                   attribute:NSLayoutAttributeLeft
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.navigationController.view
                                                                                   attribute:NSLayoutAttributeLeft
                                                                                  multiplier:1.0
                                                                                    constant:0]];
        [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
                                                                                   attribute:NSLayoutAttributeTop
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.navigationController.view
                                                                                   attribute:NSLayoutAttributeTop
                                                                                  multiplier:1.0
                                                                                    constant:0.0f]];
        [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
                                                                                   attribute:NSLayoutAttributeWidth
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.navigationController.view
                                                                                   attribute:NSLayoutAttributeWidth
                                                                                  multiplier:1.0
                                                                                    constant:0.0]];
        [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:groupView
                                                                                   attribute:NSLayoutAttributeHeight
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.navigationController.view
                                                                                   attribute:NSLayoutAttributeHeight
    }

                                                                      multiplier:1.0
                                                                                constant:0.0]];
//Animate View
[UIView animateWithDuration:0.2 animations:^(void) {
        groupView.alpha = 1.0;

    }];

我如何尝试删除视图

- (void)closeFinishAlert{
    UIView *grouView = [self.navigationController.view viewWithTag:999];
    grouView.hidden = YES;
    NSLog(@"Check if closeFinishAlert is called");
    //[self.navigationController popViewControllerAnimated:YES];
}

我也尝试过什么

这将删除视图及其内容,但在此之后,无法进行用户交互。它看起来像崩溃,但Xcode告诉我该应用程序仍在运行。

- (void)closeFinishAlert{
    UIView *groupView = [self.navigationController.view viewWithTag:999];
    for (UIView *subview in groupView.subviews) {
        subview.hidden = YES;
        [subview removeFromSuperview];
    }
    [_groupView removeFromSuperview];
    //[self.navigationController popViewControllerAnimated:YES];
}

如果您实际上只是呈现一个稍后肯定会删除的view
那么最好为您的view创建一个 ViewController 类并使用以下方式呈现它:

[self presentViewController:yourViewControllerObject animated:NO completion:nil];

您可以稍后使用以下方法删除此view

[self dismissViewControllerAnimated:NO completion:nil];

希望这有帮助!

多亏了"GoGreen"绿色,我找到了一个适合我的解决方案。

从 ViewDidAppear 创建视图控制器

- (void)viewDidAppear:(BOOL)animated{
    if (executedExercises.count == [_fetchedResultsController.fetchedObjects count] && self.groupView == FALSE) {
        [self performSelector:@selector(doneWithTraining) withObject:nil afterDelay:0.0];
    }
}

头文件

@property (strong, nonatomic) UIViewController *groupView;

创建视图滚动器及其内容

-(void)doneWithTraining{
         _groupView = [[UIViewController alloc] init];
        [self presentViewController:_groupView animated:YES completion:nil];
        _groupView.view.backgroundColor = [UIColor whiteColor];

    //SubTitle
    UILabel *subTitleLabel = [[UILabel alloc] init];
    [subTitleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:18.0]];
    subTitleLabel.textColor = [BackgroundLayer gray];
    [subTitleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    subTitleLabel.numberOfLines = 5;
    subTitleLabel.adjustsFontSizeToFitWidth = YES;
    subTitleLabel.textAlignment = NSTextAlignmentCenter;
    [_groupView.view addSubview:subTitleLabel];
    subTitleLabel.text = subTitleText;

    [_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:NSLayoutRelationLessThanOrEqual
                                                                   toItem:_groupView.view
                                                                attribute:NSLayoutAttributeWidth
                                                               multiplier:0.0
                                                                 constant:300.0f]];
    [_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:_groupView.view
                                                                attribute:NSLayoutAttributeHeight
                                                               multiplier:0.0
                                                                 constant:140]];
    [_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
                                                                attribute:NSLayoutAttributeCenterX
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:_groupView.view
                                                                attribute:NSLayoutAttributeCenterX
                                                               multiplier:1.0
                                                                 constant:0.0]];
    [_groupView.view addConstraint:[NSLayoutConstraint constraintWithItem:subTitleLabel
                                                                attribute:NSLayoutAttributeCenterY
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:_groupView.view
                                                                attribute:NSLayoutAttributeCenterY
                                                               multiplier:1.0
                                                                 constant:0.0]];}

删除视图控制器

- (void)closeFinishAlert{    
    [_groupView dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

最新更新