[英]How to add a static ad Banner into a UITableViewController?
我希望在我的應用中添加AdMob橫幅。 我把它們放在每個屏幕的底部,它可以正常工作,但在UIViewControllers方面並不完全。
當我在UITableViewController上添加一個add時,它會從屏幕底部的正確位置開始,但是當我滾動它時,它隨之移動。 當我滾動表格時,我需要廣告靜態地停留在屏幕的底部。
這是我的代碼:
- (void)displayGAD
{
// The frame of the banner is initialized off screen.
// If an ad loads then it will animate onto the screen.
self.bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0,
self.view.frame.size.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
self.bannerView.adUnitID = self.adUnitID;
self.bannerView.delegate = self;
self.bannerView.rootViewController = self;
[self.view addSubview:self.bannerView];
[self.bannerView loadRequest:[self createRequest]];
}
- (GADRequest *)createRequest
{
GADRequest *request = [GADRequest request];
#warning Comment this out before distribution
request.testDevices = [NSArray arrayWithObjects:@"84ea3d9789cabb0a34176cbb52c0f992", @"abf08fe141b95987d27ac068602605b8", GAD_SIMULATOR_ID, nil];
return request;
}
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
NSLog(@"Received Ad");
[UIView animateWithDuration:1.0 animations:^ {
view.frame = CGRectMake(0.0,
self.view.frame.size.height - view.frame.size.height,
view.frame.size.width,
view.frame.size.height);
}];
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}
我已經看到了關於如何將廣告嵌入到表格視圖中的多個示例,但沒有關於我想如何實現它的具體內容。 關於這一點,我唯一看到的是我應該將表視圖放入容器視圖中,然后將廣告添加到容器視圖中。 我不知道我會怎么做,因為這是一個UITableViewController。
我發現最簡單的方法是不使用專用的UITableViewController。 我創建了一個UIViewController並在視圖中添加了一個容器控制器。 從那里我將容器控制器子類化為UITableViewController。 我只是將所有與表視圖相關的代碼放在該子類中。 然后,我將廣告的加載和放置放在頂級UIViewController中。 這樣做意味着廣告嵌入到與容器控制器相同的視圖中。 我剛剛制作它,以便我的廣告橫幅位於容器的頂部。 這使我能夠滾動表格視圖和廣告橫幅不移動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.