IAd below UITableViewController inside UINavigationController

I am creating an application for a blog site.

I have a UINavigationController with a UITableViewController as the root view.

I did not find this in the storyboard, but I'm trying to drag the iAd view to the bottom of the screen, and xcode will not let me add it.

It seems like I need to switch from a subclass of UITableViewController to a subclass of UIViewController and just put my delegation methods and datasource in my subclass UIViewController.

This seems to me wrong. I'm just trying to get a UITableView of article titles, with a navigation bar at the top and iAd at the bottom ...

Tips? Suggestions?

Thanks in advance.

+7
source share
3 answers

One of the easiest ways to achieve this is to use the UITableView property of tableFooterView . Yes, I know that the footer is at the bottom of the table, but this is not necessary. You can set its frame in the table. Add iAd to the footer as follows:

 self.tableView.tableFooterView = iAd; 

Then, to configure the iAd frame as a scrolling table, implement the UIScrollView delegate method: (This is possible because UITableView is a subclass of UIScrollView )

 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGRect iAdFrame = iAd.frame; CGFloat newOriginY = table.contentOffset.y + table.frame.size.height - iAdFrame.size.height; CGRect newIAdFrame = CGRectMake(iAdFrame.origin.x, newOriginY, iAdFrame.size.width, iAdFrame.size.height); iAd.frame = newIAdFrame; } 

You can see that the implementation is quite simple. We just use contentOffset y to determine how far the iAd frame should be.

+23
source

I tried using the NJones example above with setting the position of tableFooterView, but I found it was difficult to control it when reloading data or updating the table.

Then I found out that this can be done by adding an iAd banner to the tableViewController supervisor .

 self.bannerViewController = [[BannerViewController alloc] init]; [self.bannerViewController.view setHidden:YES]; [self.bannerViewController.view setFrame:CGRectMake(0, self.view.superview.frame.size.height - self.tabBarController.tabBar.frame.size.height - 50, 320, 50)]; [self.view.superview addSubview:self.bannerViewController.view]; [self.bannerViewController loadBanner]; 

When the banner loads, I create a tableFooterView to free up space for the last cell in the ViewController table

 -(void)bannerDidLoad{ [self.bannerViewController.view setHidden:NO]; self.tableView.tableFooterView = [[UIView alloc]; initWithFrame:self.bannerViewController.view.frame]; } 
+2
source

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; } 
0
source

All Articles