I had to make some changes to the solution sent by NJones, as there was a problem with the fact that the ad was not showing on top of all other cells / views.
First make sure your TableViewController is an AdBannerViewDelegate:
@interface MyTableViewController () <ADBannerViewDelegate>
Adding AdBanner to tableviewcontroller:
- (void)viewDidLoad { [super viewDidLoad]; ... ADBannerView *adBanner = [[ADBannerView alloc]initWithAdType:ADAdTypeBanner]; adBanner.delegate = self; self.tableView.tableFooterView = adBanner; }
The code for placing the advertising banner is taken from NJones, I added only the last line to add the advertising banner to the beginning:
-(void)positionAdBanner { ADBannerView *adBanner = (ADBannerView *) self.tableView.tableFooterView; if (adBanner) { CGRect iAdFrame = adBanner.frame; CGFloat newOriginY = self.tableView.contentOffset.y + self.tableView.frame.size.height - iAdFrame.size.height; CGRect newIAdFrame = CGRectMake(iAdFrame.origin.x, newOriginY, iAdFrame.size.width, iAdFrame.size.height); adBanner.frame = newIAdFrame; [self.tableView bringSubviewToFront:adBanner]; } }
This function is called whenever the view collects the layout of its subzones (so you only need here, there is no need to check scrolling, etc.):
-(void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; [self positionAdBanner]; }
You must also handle the ADBannerViewDelegate methods:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner { banner.hidden = NO; [self positionAdBanner]; } -(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { banner.hidden = YES; }
Cesar
source share