[英]AdMob banner view background color is black, want white or transparent
您可以将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的信息中心实现的。
更改可能需要几分钟才能显示在您的应用程序中。
您首先需要弄清楚组成广告视图的视图。 可能是您在更改视图的颜色,但该视图的顶部还有另一个包含广告的视图,并且总计阻止了父视图。
首先在代码中放置一个断点(我通常将其放置在-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.