您应该将其添加GADBannerView
到您的视图中并将其隐藏属性设置为YES
最初。另外,我建议使用kGADAdSizeBanner
AdMob 提供的 AdSize 常量。以下是其他AdSize 常量的列表。
例如:
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.adUnitID = @"YourAdUnitID";
bannerView.rootViewController = self;
bannerView.delegate = self;
[bannerView loadRequest:[GADRequest request]];
bannerView.hidden = YES; // Hide banner initially
[self.view addSubview:bannerView];
// This will put the banner at the bottom of the screen and stretch to fit the screens width
[bannerView setFrame:CGRectMake(0, self.view.frame.size.height - bannerView.frame.size.height, self.view.frame.size.width, bannerView.frame.size.height)];
然后,当您收到广告时,您会取消隐藏横幅。例如:
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
// We've received an ad so lets show the banner
bannerView.hidden = NO;
NSLog(@"adViewDidReceiveAd");
}
-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
// Failed to receive an ad from AdMob so lets hide the banner
bannerView.hidden = YES;
NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}
如果您愿意,您还可以通过将横幅的alpha
属性设置为0.0
最初而不是使用它的hidden
属性来制作动画。alpha
然后,在您收到广告时制作动画。例如:
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
// We've received an ad so lets fade in the banner
[UIView animateWithDuration:0.2 animations:^{
bannerView.alpha = 1.0;
}];
NSLog(@"adViewDidReceiveAd");
}
-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
// Failed to receive an ad from AdMob so lets fade out the banner
[UIView animateWithDuration:0.2 animations:^{
bannerView.alpha = 0.0;
}];
NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}
此外,作为旁注,GADBannerView
当没有广告可显示时,它是透明的。因此,将其添加到您的视图中并且什么也不做也可以。