我用它在 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 键盘键入了单词,则上面的代码有效。但是,当我在模拟器中打开软件键盘时,广告无法加载到页脚。相反,它直接加载到搜索栏下方。我应该如何解决这个问题?不知道这个bug在真机上有没有出现。
最佳答案
我需要更高级别的代表来发表评论,但问题可能与您创建 GADBannerView 的方式有关。
如您所知,只要需要呈现页脚 View ,就会调用委托(delegate) -tableView:viewForFooterInSection:
。如果您将此 View 滚动到屏幕外并返回,委托(delegate)将再次触发。
在 -tableView:viewForFooterInSection:
委托(delegate)方法中创建 GADBannerView 将导致每次来回滚动时创建新的广告 View ,并发送新的广告请求。
我的建议是将 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
即使我在下面的模糊猜测对您没有帮助,也许这个示例可以帮助您保留相同的广告 View ,并且不会在用户每次来回滚动时更改广告,从而对用户更好一些。
我的模糊猜测:
我从未使用过 Google Ads,所以我无法告诉您他们是否尝试在未正确附加到 View 层次结构的情况下处理广告。我怀疑分离的广告(被键盘覆盖以便加载新广告)以某种方式放错了位置。
关于ios - 显示键盘时,Admob 广告不会出现在 UITableView 的页脚中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158315/