问题
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.
回答1:
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.
回答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];
}
回答3:
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;
}
来源:https://stackoverflow.com/questions/9857625/iad-below-uitableviewcontroller-inside-uinavigationcontroller