[英]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.