显示键盘时,Admob广告不会显示在UITableView的页脚中

我使用它在UITableView的页脚上显示Admob广告:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { GADBannerView *sampleView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { sampleView.hidden=true; } sampleView.adUnitID = @"myID"; sampleView.rootViewController = self; [sampleView loadRequest:[GADRequest request]]; return sampleView; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 50.0; } 

我没有真正的iPhone设备,所以我只在模拟器上测试它。 如果软件键盘被隐藏并且我使用MacBook键盘输入了单词,则上面的代码可以正常工作。 但是,当我在模拟器中打开软件键盘时,广告无法在页脚加载。 相反,它会在搜索栏下方加载。 我该怎么解决这个问题? 我不知道这个错误是否也发生在真实设备中。

Admob没有页脚

我需要一个更高的代表来评论,但问题可能与你如何创建GADBannerView有关。

如您所知,只要需要呈现页脚视图,就会调用委托-tableView:viewForFooterInSection: . 如果您将此视图从屏幕滚动到何处,则会再次触发该委托。

-tableView:viewForFooterInSection: delegate方法中创建GADBannerView将导致每次来回滚动时都会创建新的广告视图,并且会发送新的广告请求。

我的建议是将GADBannerView创建移动到ViewController的-viewDidLoad:方法,并且每次都返回相同的实例。

 @interface MyViewController () @property (nonatomic, strong) GADBannerView *footerAdView; @property (nonatomic, assign, getter=isFooterAdRequested) BOOL footerAdRequested; @end @implementation MyViewController ... -(void)viewDidLoad:(BOOL)animated { self.footerAdView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner]; _footerAdView.adUnitID = @"myID"; _footerAdView.rootViewController = self; } ... - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if(![self isFooterAdFetched]) { [_footerAdView loadRequest:[GADRequest request]]; self.footerAdRequested = YES; // Set this to NO again when ad is dismissed/closed/ended so that a new ad can be requested only when the current one is done. } return _footerAdView; } ... @end 

即使我下面的模糊猜测对您没有帮助,也许这个示例可以帮助您保留相同的广告视图,并且每次用户来回滚动时都不会更改广告,从而对用户更好一点。

我的模糊猜测:

我从未使用Google广告,因此我无法告诉您他们是否尝试处理广告而未正确附加到视图层次结构。 我怀疑是分离的广告(由键盘覆盖以便加载新的广告)不知何故被错放了。