Add GADBannerView Programmatically

2019-04-09 09:47发布

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];

5条回答
叼着烟拽天下
2楼-- · 2019-04-09 09:56

Inside the delegate callback adViewDidReceiveAd:, check the size of the ad and move your views accordingly

查看更多
等我变得足够好
3楼-- · 2019-04-09 10:02

This is an aside, because my problem looked similar, but was not.

App ID is NOT the same as Ad Unit ID.

And, stupidly, they BOTH start with ca-app-pub- and a lot of numbers. I was using the App ID which I can guarantee 100% does NOT work.

Yes, I feel pretty silly, but the folks at Google should have made them dissimilar.

However, if you use the Ad Unit ID, surprisingly, everything works. :)

查看更多
孤傲高冷的网名
4楼-- · 2019-04-09 10:04

This worked for me:

GADBannerView *gv=[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
[self.view addSubview:gv];

gv.adUnitID = @"ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx";
gv.rootViewController = self;
gv.delegate=(id<GADBannerViewDelegate>)self;
GADRequest *request = [GADRequest request];
// Enable test ads on simulators.
request.testDevices = @[ GAD_SIMULATOR_ID ];
[gv loadRequest:request];
查看更多
家丑人穷心不美
5楼-- · 2019-04-09 10:06

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
查看更多
祖国的老花朵
6楼-- · 2019-04-09 10:10

Well I think you have multiple things to consider:

  1. [sender.view addSubview:bannerView] does not move other views, instead you are placing the banner view directly on top of the view hierarchy.

  2. Ads are loaded asynchronously, so you should implement the delegate methods of GADBannerView for showing and hiding the ad view.

  3. If you really want to move other views around you should use some kind of container view to include all other content except your banner. If you receive an ad you can easily move the container view (or shrink it) and display your banner view on some location in your main view.

Cheers

查看更多
登录 后发表回答