2
votes

I am animating (for 0.5 seconds) an iAd banner off the bottom of the screen and then on completion removeing it from it's superview.

As long as I have the animation the iAd obstruction earning comes up in the console:

ADBannerView: WARNING A banner view has an ad but may be obscured. This message is only printed once per banner view.

The code is simply: (the line in the animations block being the problem)

[UIView animateWithDuration:0.5 animations:^{
    self.adBannerView.frame = CGRectSetY(self.adBannerView.frame, viewHeight);
} completion:^(BOOL finished) {
    [self.adBannerView removeFromSuperview];
}];

If I just ignore the message will it affect the number of ads or anything that my app recieves?

This also happened when I was animating the appearance of an ad from the -(void)bannerViewDidLoadAd:(ADBannerView *)banner delegate method, I could only stop the warnign from showing while keeping the animation by using the will variation of that method.

How is animating iAds on and off screen meant to be done?

2

2 Answers

5
votes

I was wondering the same thing for a while. I believe I've come up with a trick, because I don't get any warnings when I do this.

Here's the view hierarchy I have:

UIView (same size as your iAd)
|_ iAd (make sure to pin the height and width if using iOS 6 auto layout)
|_ UIView (I create this dynamically and use it's presence to determine whether I should show or hide the iAd from the delegate)

The code below manipulates the auto layout constraints I have setup in Interface Builder. If you're not using auto layout, then you'll have to change what triggers the animation.

- (void)hideAdBanner {
    if (!__adBannerReverseSideView) {
        __adBannerReverseSideView = [[UIView alloc] initWithFrame:__adBannerView.frame];
        __adBannerReverseSideView.backgroundColor = [UIColor blackColor];
        __adBannerReverseSideView.opaque = YES;

        [UIView transitionFromView:__adBannerView toView:__adBannerReverseSideView duration:0.3
                options:UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionCurveEaseInOut
                completion:^(BOOL finished) {
                    [UIView animateWithDuration:0.3  animations:^{
                        __adBannerBottomConstraint.constant += __adBannerContainerView.frame.size.height;
                        [self.view layoutIfNeeded];
                    }];
                }];
    }
}

- (void)showAdBanner {
    if (__adBannerReverseSideView) {
        [UIView animateWithDuration:0.3
            animations:^{
                __adBannerBottomConstraint.constant -= __adBannerContainerView.frame.size.height;
                [self.view layoutIfNeeded];
            }
            completion:^(BOOL finished) {
                [__adBannerView setNeedsLayout];
                [UIView transitionFromView:__adBannerReverseSideView toView:__adBannerView duration:0.3
                        options:UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionCurveEaseInOut
                        completion:^(BOOL finished) {
                           [__adBannerReverseSideView removeFromSuperview];
                            __adBannerReverseSideView = nil;
                        }];
            }];
    }
}

The hide code transitions the AD Banner to the "reverse" view. You can change the animation types with the options parameter.

The show code transitions the other way (from "reverse" view to AD Banner). All of the animation happens with the superview which is the same size as the AD Banner. This way your entire view won't animate.

Leave the iAd in the superview, don't remove it. This may be the root cause of the warning, but I'm not sure.

Then here are my AD Delegate methods:

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [self hideAdBanner];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [self showAdBanner];
}

Don't kill me for not checking the error variable. I haven't gotten around to writing that code just yet.

With regard to pinning the height and width of the ad banner view in iOS 6 auto layout, if you don't when the iAd animates back into place, the upper left hand corner will shift down and to the right by half the height and width of its superview every time it comes back into view. :) Fun stuff.

0
votes

The warning won't do anything bad to your app. Ads will load whether or not it's onscreen and delegate methods will be called. You're doing it correctly. The warning is just a suggestion because some people might want an ad to show up despite certain view resizes and such, so it just suggests that the ad should always be onscreen. But again, it's a suggestion, not an error.