Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 641
overeasy Avatar asked Mar 25 '12 03:03

overeasy


2 Answers

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.

like image 181
NJones Avatar answered Nov 09 '22 05:11

NJones


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];
 }
like image 27
user1506145 Avatar answered Nov 09 '22 05:11

user1506145