Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add GADBannerView Programmatically

I created a class as AdMobViewController and I use to add banner this class. Banner create but other objects in view don't scroll up. How I do programmatically scroll up all objects in view.

My AdMobViewController class and method:

+ (void)createBanner:(UIViewController *)sender
{
    GADRequest *request = [GADRequest request];
    request.testing = YES;
    request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];

    GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    bannerView.adUnitID = @"ca-app-pub-123456";
    bannerView.rootViewController = (id)self;
    bannerView.delegate = (id<GADBannerViewDelegate>)self;
    bannerView.frame = CGRectMake(0, 518, 320, 50);

    [bannerView loadRequest:request];

    [sender.view addSubview:bannerView];
}

And I use for creating banner:

[AdMobViewController createBanner:self];
like image 530
Yasin Uğur Yıldız Avatar asked Feb 13 '14 16:02

Yasin Uğur Yıldız


1 Answers

Working sample copy is here:

For using: [AdMobViewController createBanner:self];

#import "AdMobViewController.h"
#import "GADBannerView.h"

@interface AdMobViewController ()

@end

@implementation AdMobViewController

static GADBannerView *bannerView;
static UIView *senderView;
static UIView *containerView;
static UIView *bannerContainerView;
static float bannerHeight;

+ (void)createBanner:(UIViewController *)sender
{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {
        bannerHeight = 50;
    }
    else
    {
        bannerHeight = 90;
    }

    GADRequest *request = [GADRequest request];
    request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];

    bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    bannerView.adUnitID = @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
    bannerView.rootViewController = (id)self;
    bannerView.delegate = (id<GADBannerViewDelegate>)self;

    senderView = sender.view;

    bannerView.frame = CGRectMake(0, 0, senderView.frame.size.width, bannerHeight);

    [bannerView loadRequest:request];

    containerView = [[UIView alloc] initWithFrame:senderView.frame];

    bannerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, senderView.frame.size.height, senderView.frame.size.width, bannerHeight)];

    for (id object in sender.view.subviews) {

        [object removeFromSuperview];
        [containerView addSubview:object];

    }

    [senderView addSubview:containerView];
    [senderView addSubview:bannerContainerView];
}

+ (void)adViewDidReceiveAd:(GADBannerView *)view
{
    [UIView animateWithDuration:0.5 animations:^{

        containerView.frame = CGRectMake(0, 0, senderView.frame.size.width, senderView.frame.size.height - bannerHeight);
        bannerContainerView.frame = CGRectMake(0, senderView.frame.size.height - bannerHeight, senderView.frame.size.width, bannerHeight);
        [bannerContainerView addSubview:bannerView];

    }];
}

@end
like image 117
Yasin Uğur Yıldız Avatar answered Sep 25 '22 10:09

Yasin Uğur Yıldız