iOS7 Auto Layout, View Resize and iAds

autolayout, iad, ios, ios7, objective-c

Solution

After you create the banner view in code (and add it as a subview of main view), you should add a 0 length spacing constraint between the bottom of the container view, and the top of the banner view (the banner view would need constraints to the two sides of the main view and a height constraint as well). The container view should have 0 length constraints to all four edges of the main view. You should make an IBOutlet to that bottom constraint, and animate that constraint's constant value by an amount equal to the height of the banner view (so it will shrink, and the banner view will move up with it due to its 0 length vertical spacing constraint). So, if the outlet to the bottom constraint was called bottomCon, and the height of the banner view was 100 points, you would animate like this:

[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        self.bottomCon.constant = 100;
        [self.mainView layoutIfNeeded];
    }];

There's no need to hide and unhide the view, since you will initially place it off the bottom of the screen anyway. Also make sure that you call `[bannerView setTranslatesAutoresizingMaskIntoConstraints:NO]` right after you create the banner view, or you'll get auto layout errors when you run the app.

Problem

I'm using Auto Layout in my iOS 7 project with the following view hierarchy Main View -Container View ---Button ---Button ---ImageView -Banner View (iAd Banner View) The Main View and Container View are full width and height of screen. I have Horizontal and Vertical Space Constraints on the Container View sticking to the main view (screen's height and width). And also the subviews of Container View are constrained to the button of the view with a 20px space. My issue occurs when the Banner View is finally filled and placed at the bottom of the screen, which then I have the Container View subtract the Banner View's Height from its frame height to allow space for the Banner View to show. (code used below) The ideal outcome is the Container View to subtract the height and its subviews constraint update based on this new height ,but what end up happening is the iAD Banner View just overlays the view as shown in the picture. Code for BannerViewDidLoadAd: ``` - (void)bannerViewDidLoadAd:(ADBannerView *)banner { CGRect contentFrame = self.containerView.bounds; CGRect bannerFrame = self.bannerView.bounds; if (self.bannerView.bannerLoaded) { contentFrame.size.height = self.containerView.frame.size.height - self.bannerView.frame.size.height; bannerFrame.origin.y = contentFrame.size.height;; } else { bannerFrame.origin.y = contentFrame.size.height; } [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ [self.containerView setFrame:contentFrame]; [self.containerView layoutIfNeeded]; self.bannerView.frame = bannerFrame; self.bannerView.hidden = NO; }]; [self.containerView updateConstraints]; } ``` Image of iAd overlaying Container View and it's SubViews

Original source