ChildViewController not animating when parent view changes constraint

| | August 18, 2015

i’m trying to get a setup where there is a view on the bottom of my app that I can animate up and down. The view should persist across view controller transitions. I created a “shell” view controller with a top view and a bottom view. I added the navigation controller as a childVC of the shell and added it’s subview to the top view. The subview wasn’t resizing correctly so I added it in a weird way…

[shellVC addChildViewController:navController];
[self properAddSubview:navController.view toParent:shellVC.topView inView:shellVC.view];
[navController didMoveToParentViewController:shellVC];

here is the properAddSubview method…

// This helper method sets the proper constraints so that the subview grows to the size of the superview
- (void) properAddSubview:(UIView*)subview toParent:(UIView*)parent inView:(UIView*)vcView{

    [parent insertSubview:subview atIndex:0];

    [subview setTranslatesAutoresizingMaskIntoConstraints:NO];

    [vcView addConstraint:[NSLayoutConstraint
                           constraintWithItem:parent
                           attribute:NSLayoutAttributeWidth
                           relatedBy:NSLayoutRelationEqual
                           toItem:subview
                           attribute:NSLayoutAttributeWidth
                           multiplier:1.0
                           constant:0.0]];

    [vcView addConstraint:[NSLayoutConstraint
                           constraintWithItem:parent
                           attribute:NSLayoutAttributeHeight
                           relatedBy:NSLayoutRelationEqual
                           toItem:subview
                           attribute:NSLayoutAttributeHeight
                           multiplier:1.0
                           constant:0.0]];

    [vcView addConstraint:[NSLayoutConstraint
                           constraintWithItem:parent
                           attribute:NSLayoutAttributeCenterX
                           relatedBy:NSLayoutRelationEqual
                           toItem:subview
                           attribute:NSLayoutAttributeCenterX
                           multiplier:1.0
                           constant:0.0]];

    [vcView addConstraint:[NSLayoutConstraint
                           constraintWithItem:parent
                           attribute:NSLayoutAttributeCenterY
                           relatedBy:NSLayoutRelationEqual
                           toItem:subview
                           attribute:NSLayoutAttributeCenterY
                           multiplier:1.0
                           constant:0.0]];
}

Now the problem is occurring when I try to animate the constraints on the shellVC. The bottomVC has an iAD view added to it. When I run the app, the ad in the bottomView of the shellVC animates up correctly, but the top navigation controller view doesn’t animate (although it goes in the correct spot). I’m calling this method in the viewWillAppear when pushing VCs to the navigation stack that is inside the top view of the shellVC. Here is my animation code inside the shellVC…

- (void) toggleAds:(BOOL)on{

    if (self.adsOn == on){
        return;
    }

    self.adsOn = on;

    [self.view layoutIfNeeded];    
    [UIView animateWithDuration:1.0 animations:^{
        [self.adViewToBottomConstraint setConstant:(on ? 0 : -1 * MOPUB_BANNER_SIZE.height)];
        [self.view layoutIfNeeded];
    }];

}

Any help would be appreciated. I know I kind of have a crazy setup here, maybe someone could suggest a better way to go about this? Basically I have it this way because I want the ads to stay on the bottom of the screen even when I transition view controllers. Also I want to be able to animate it up and down on certain VCs and also when no ad is available.

Leave a Reply