繁体   English   中英

AdMob横幅视图背景颜色为黑色,希望为白色或透明

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

当广告不合适时,我不知道如何更改AdMob横幅的背景颜色。 这可能吗? 下面的代码对我不起作用。

self.ad.backgroundColor= [UIColor whiteColor];

在此处输入图片说明

您可以将GADBannerView添加到设置backgroundColor的另一个UIView中。 这将需要使用AdMob的默认尺寸来确保图片横幅正确放置。 这样做的不足之处在于,基于文字的广告也将被限制为该尺寸,而不是填充整个广告区域。

例如:

#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

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

实际上,这是通过AdMob的信息中心实现的。

  1. 前往AdMob.com
  2. 选择顶部工具栏上的获利
  3. 在左侧栏中选择要更改其背景颜色的应用程序
  4. 选择横幅的广告单元
  5. 文字广告样式下拉菜单中,选择自定义
  6. 选择背景色并将其更改为所需的任何颜色
  7. 选择“ 保存” ,您就完成了

更改可能需要几分钟才能显示在您的应用程序中。

在此处输入图片说明

您首先需要弄清楚组成广告视图的视图。 可能是您在更改视图的颜色,但该视图的顶部还有另一个包含广告的视图,并且总计阻止了父视图。

首先在代码中放置一个断点(我通常将其放置在-viewDidAppear: ),然后在调试器中键入该断点以查看广告视图的子视图:

po self.ad.subviews

我的猜测是,您将看到一个子视图,该子视图会扩展主窗口的整个长度。 它甚至可能是包含广告图像的UIImageView 但是,您可以继续在调试器中或通过在-viewDidAppear:添加代码来查看子视图的子视图。

这可能是您想要弄乱的视图。 -viewDidAppear: ,尝试添加代码为子视图的背景着色:

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

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

即使广告或其subviews属性返回nil此代码也应该是安全的。

您应该将广告插入到容器视图中。

然后,在加载广告时调用的委托中,将容器宽度调整为与广告视图相同的大小

我认为您应该填充相同的Admob大小的UIView。

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

框架大小(rectBanner)应与Admob大小匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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