简体   繁体   中英

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

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

在此处输入图片说明

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

在此处输入图片说明 在此处输入图片说明

This is actually achieved through AdMob's dashboard.

  1. Go to AdMob.com
  2. Select Monetize on the top toolbar
  3. Select the application on the left side bar that you want to change the background color for
  4. Select the Ad Unit of the banner
  5. In the Text ad style drop down menu select Customized
  6. Select Background color and change it to whichever color you desire
  7. Select Save and you're all done

It may take a few minutes for changes to appear in your application.

在此处输入图片说明

You need to start by figuring out what views compose the ad view. Probably you are changing the view's color, but it has another view on top of it that contains the ad, and total blocks the parent view.

Start by placing a breakpoint in your code (I often place it in -viewDidAppear: ) and once in there, type this in the debugger to see the subviews of the ad view:

po self.ad.subviews

My guess is that you'll see one subview that stretches the full length of the main window. It may even be a UIImageView that contains the ad image. But you can continue to look at the subviews of the subviews, either in the debugger or by adding code in -viewDidAppear: .

This is probably view you want to mess with. In -viewDidAppear: , try adding code to color the subview's background instead:

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

    UIView *adSubview = self.ad.subviews.firstObject;
    adSubview.backgroundColor = [UIColor whiteColor];
}

This code should be safe even if the ad or its subviews property returns nil .

You should insert the ad in a container view.

Then, in the delegate that is called when the ad is loaded adjust the container width to be the same size as the ad view

I think you should fill rect of UIView same Admob size.

bannerView = GADBannerView(frame: rectBanner)
bannerView.adUnitID = "xxxxxxxxxxxxxxxxxx"
bannerView.rootViewController = self

frame size(rectBanner) should be match with Admob Size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM