Search code examples
ios5uinavigationcontrolleruitableviewiad

iAd below UITableViewController inside UINavigationController


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.


Solution

  • 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.