7

I'm building an app for a blog site.

I have a UINavigationController with a UITableViewController as it's root view.

I laid this out in a storyboard no problem, but I'm trying to drag an iAd view to the bottom of the screen and xcode will not let me add it.

It looks like I have to switch from a subclass of UITableViewController to a subclass of UIViewController, and just put my delegate and datasource methods in my subclassed UIViewController.

This seems wrong to me. I'm just trying to end up with a UITableView of article headlines, with a navbar up top, and an iAd at the bottom...

Advice? Suggestions?

Thanks in advance.

overeasy
  • 404
  • 5
  • 12

3 Answers3

23

One of the easiest ways to accomplish this is using the UITableView's tableFooterView property. Yes, I know the footer stays at the bottom of the table, but it doesn't have to. You can set its frame within the table. Add the iAd as the footer like so:

self.tableView.tableFooterView = iAd; 

Then, to adjust the frame of the iAd as the table scrolls, 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 easy enough. We simply use the contentOffset y to determine how far down the frame of the iAd should be.

AmitaiB
  • 1,656
  • 1
  • 19
  • 19
NJones
  • 27,139
  • 8
  • 70
  • 88
  • 1
    this worked thanks. for the record however, I ended up simply using a viewController and a tableView instead of using a tableViewController, so I had no problem resizing the tableView and placing the iAd below. – overeasy Apr 04 '12 at 23:52
  • I have no idea how to make this code work in my application. I've dragged the banner view in the table view footer of my table view, but I'm not sure where to go from there.. This variable `iAd` where is that being assigned? – The Muffin Man Jun 17 '13 at 06:44
  • I see, I just initialized a new `AdBannerView` programmatically. – The Muffin Man Jun 17 '13 at 06:48
  • 1
    Thnx NJones! That worked for me! I also tried this in viewDidAppear method, so you don't have to scroll in order to show the ad and it worked there too – Apostolos Jun 26 '13 at 21:50
  • I realize this thread is a bit old, but just wanted to update... I could not quite get this working on iOS6. If I just used this in the table footer, then the height of the footer would be off, although the alignment appeared to be working at times. Once I set the height via the table:heightForFooter delegate, this didn't appear to work at all. – Danny Sung Jul 18 '13 at 03:55
  • This think is working fine for me in iOS 7 and I don't have a footer I just setup ADBannerView in viewDidLoad and have scrollViewDidLoad as the snippet says. The moment user scrolls the banner appears at the bottom and always being at the bottom. – Maziyar Jul 07 '14 at 13:46
  • This works to get the ad to appear when I start scrolling, but I am unable to get the ad to appear before the user scrolls. Any idea why? – Jacob Jul 24 '14 at 03:41
  • This worked; however, I had to add to `[table bringSubviewToFront:iAd]` to keep the ad at the bottom and over the table row content. – johnnieb May 28 '15 at 22:02
2

I tried to use the example above by NJones with adjusting the position of the tableFooterView, but I found out it was hard to manage it when reloading the data or refreshing the table.

Then I found out that this could be done by adding the iAd banner to the superview of the tableViewController's view.

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 is loaded I create a tableFooterView to make space for the last cell in the tableViewController

-(void)bannerDidLoad{
     [self.bannerViewController.view setHidden:NO];
     self.tableView.tableFooterView = [[UIView alloc];
     initWithFrame:self.bannerViewController.view.frame];
 }
user1506145
  • 5,176
  • 11
  • 46
  • 75
0

I had to make some changes to the solution posted by NJones, since there was a problem with the ad not being displayed on top of all other cells/views.

First make sure your tableViewController is a AdBannerViewDelegate:

@interface MyTableViewController () <ADBannerViewDelegate>

Adding the AdBanner to the tableviewcontroller:

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    ADBannerView *adBanner = [[ADBannerView alloc]initWithAdType:ADAdTypeBanner];
    adBanner.delegate = self;
    self.tableView.tableFooterView = adBanner;
}

The code to position the ad banner is taken from NJones, I only added the last line to bring the ad banner to the front:

-(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 gets called whenever the view is going to layout its subviews (so you only need it here, no need to check for scrolling, etc):

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    [self positionAdBanner];
}

You also should handle the ADBannerViewDelegate methods:

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    banner.hidden = NO;
    [self positionAdBanner];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    banner.hidden = YES;
}
Cesar
  • 2,027
  • 2
  • 18
  • 29