0
votes

I have a Navigationviewcontroller with a tableview in it. I am adding a UIView to self.navigationcontroller.view and constraints for Auto Layout. When I try to remove it from superview or to hide it, nothing happens.

Any ideas?

How I create the View and Constraints

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


    }];

How I try to remove the View

- (void)closeFinishAlert{

    UIView *grouView = [self.navigationController.view viewWithTag:999];
    grouView.hidden = YES;

    NSLog(@"Check if closeFinishAlert is called");
    //[self.navigationController popViewControllerAnimated:YES];
}

What I also tried

This removes the view an its content, but after this there is no user interaction possible. It looks like a crash, but Xcode tells me that the app is still running.

- (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
Why are you adding a subview to the navigation controller? you should be adding a UIView to your viewController's view. If you tell us the nature of your view that you are trying to add and remove then we can possibly provide a better way of dealing with your situation - Pavan
When I try to add anything to self.view the app always crashes due to auto layout. - Jan
Take a look at this post: stackoverflow.com/questions/11198981/… you need to make sure you turn off "use auto layout" for your nibs. Then try adding your views with [self.view addSubview:yourView]; - Pavan
Turning off auto layout is no option, I need auto layout since this app supports iPhone and iPad as well as all orientations. - Jan
And adding views to your UINavigationController is no option either. Either add your views properly and solve the crashes you get when adding your views or turn off Auto layout; let me tell you that the latter is definitely not an option when you have the former as your problem :) - Pavan

2 Answers

0
votes

If you are actually just presenting a view which you will remove for sure later on,
then its better to create a ViewController class for your view and present it modally using :

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

you can later on remove this view using:

[self dismissViewControllerAnimated:NO completion:nil];

hope this helps !

0
votes

Thanks to "GoGreen" green I found a solution which works for me.

Create Viewcontroller from ViewDidAppear

- (void)viewDidAppear:(BOOL)animated{

    if (executedExercises.count == [_fetchedResultsController.fetchedObjects count] && self.groupView == FALSE) {

        [self performSelector:@selector(doneWithTraining) withObject:nil afterDelay:0.0];
    }
}

Header file

@property (strong, nonatomic) UIViewController *groupView;

Creating the Viewcrontroller and its content

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

Removing ViewController

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

}