Search code examples
sprite-kitiadadbannerviewbanner-ads

Cannot seem to move iAd banner to the top of the screen with this code


I have searched and searched and have done lots of trial and error on this code trying to move the ad banner to the top of the screen. It has been a week already with no progress. If anyone could help me out with this issue I would be thrilled. The code this is based off of is by the gentleman's great tutorial: http://codewithchris.com/iad-tutorial/

Here's the code:

@interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!_bannerIsVisible)
    {
    // If banner isn't part of view hierarchy, add it
    if (_adBanner.superview == nil)
    {
        [self.view addSubview:_adBanner];
    }

        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];

        // Assumes the banner view is just off the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);

        [UIView commitAnimations];

        _bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"Failed to retrieve ad");

if (_bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];

        // Assumes the banner view is placed at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);

        [UIView commitAnimations];

        _bannerIsVisible = NO;
    }
}

@end

Solution

  • Change this line:

    _adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
    

    To this:

    _adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];
    

    (where the -50 is the height of the ad banner.) Before you were setting the ad banner's frame to the bottom of the screen using self.view.frame.size.height, whereas now the -50 moves the frame just above the screen.

    Also, don't forget you'll need to reverse your CGRectOffsets' y coordinates (remove the (-) from the first and add it to the second) because the ones you have at the moment assume the frame is at the bottom of the screen.

    Hope this helps!