Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMob banner view background color is black, want white or transparent

Tags:

ios

admob

I can't figure out how to change my AdMob banner's background color when the ad doesn't fit. Is this possible? The code below wouldn't work for me.

self.ad.backgroundColor= [UIColor whiteColor];

enter image description here

like image 323
Mike Flynn Avatar asked Oct 15 '25 14:10

Mike Flynn


1 Answers

You could add your GADBannerView to another UIView that you set the backgroundColor of. This would require using AdMob's default sizes to make sure that image banners fit properly. The down fall of this is that text based ads will be restricted to this size also instead of filling the entire ad area.

For example:

#import "ViewController.h"
@import GoogleMobileAds;

#define ADUNIT_ID @"yourAdUnitID"

@interface ViewController () <GADBannerViewDelegate> {
    GADBannerView *admobBanner;
    UIView *backgroundView;
}

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];

    // Create our AdMob banner
    admobBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    admobBanner.adUnitID = ADUNIT_ID;
    admobBanner.rootViewController = self;
    admobBanner.delegate = self;
    [admobBanner loadRequest:[GADRequest request]];

    // Create a view to put our AdMob banner in
    backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0,
                                                            self.view.frame.size.height - admobBanner.frame.size.height,
                                                            self.view.frame.size.width,
                                                            admobBanner.frame.size.height)];
    // Hide view until we have an ad
    backgroundView.alpha = 0.0;

    // Set to color you require
    backgroundView.backgroundColor = [UIColor redColor];

    // Add our views
    [self.view addSubview:backgroundView];
    [backgroundView addSubview:admobBanner];

    // Center our AdMob banner in our view
    admobBanner.center = [backgroundView convertPoint:backgroundView.center fromView:backgroundView.superview];
}

-(void)adViewDidReceiveAd:(GADBannerView *)adView {
    NSLog(@"adViewDidReceiveAd");
    [UIView animateWithDuration:0.5 animations:^{
        backgroundView.alpha = 1.0;
    }];
}

-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
    [UIView animateWithDuration:0.5 animations:^{
        backgroundView.alpha = 0.0;
    }];
}

iPhone / iPad

enter image description here enter image description here

like image 73
Daniel Storm Avatar answered Oct 18 '25 08:10

Daniel Storm