0

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];
}
Jan
  • 21
  • 1
  • 6
  • 3
    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 Jan 16 '14 at 19:20
  • When I try to add anything to self.view the app always crashes due to auto layout. – Jan Jan 16 '14 at 19:43
  • Take a look at this post: http://stackoverflow.com/questions/11198981/presentviewcontroller-crash-on-ios-6-autolayout 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 Jan 16 '14 at 20:08
  • Turning off auto layout is no option, I need auto layout since this app supports iPhone and iPad as well as all orientations. – Jan Jan 16 '14 at 20:17
  • 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 Jan 16 '14 at 21:51

2 Answers2

0

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 !

GoGreen
  • 2,251
  • 1
  • 17
  • 29
  • Bam! This could be the solution. I will try this. – Jan Jan 17 '14 at 07:29
  • I tried it, but I am getting the following error: NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint items must each be an instance of UIView or subclass' Is there a specific way to add auto layout constraints on a programmatically created view? – Jan Jan 17 '14 at 18:24
0

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

}
Jan
  • 21
  • 1
  • 6