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

时间:2014-01-16 19:04:26

标签: ios objective-c tableview autolayout

我有一个带有tableview的Navigationviewcontroller。我正在为自动布局的self.navigationcontroller.view和约束添加一个UIView。当我尝试将其从superview中删除或隐藏它时,没有任何反应。

有什么想法吗?

我如何创建视图和约束

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

2 个答案:

答案 0 :(得分:0)

如果你实际上只是提出一个view,你将在以后删除,  那么最好为ViewController创建一个view课程并使用以下方式呈现它:

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

稍后您可以使用

删除此view
[self dismissViewControllerAnimated:NO completion:nil];

希望这有帮助!

答案 1 :(得分:0)

感谢" GoGreen"绿色我发现了一个适合我的解决方案。

从ViewDidAppear创建Viewcontroller

- (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;

创建Viewcrontroller及其内容

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

删除ViewController

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

}