我无法让iAd在UITableViewController中的故事板中工作。
到目前为止,我基本上就是这样做的:在故事板中,我在UITableViewController的场景底部拖动了一个iAd横幅视图。(我无法在视图中设置横幅,因为情节提要阻止了它。我只能将iAd对象拖动到第一个响应程序和UITableViewController图标旁边。)
在UITAbleViewController的标题中,我有:
#import <UIKit/UIKit.h>
#import "ListViewController.h"
#import <iAd/iAd.h>
@interface ListViewController : UITableViewController <ADBannerViewDelegate>{
bool bannerIsVisible;
}
@property (strong, nonatomic) IBOutlet ADBannerView *iAd;
@end
在UITableViewController的实现中,我有:
- (void)viewDidLoad
{
[super viewDidLoad];
iAd.delegate = self;
// iAd.frame = CGRectMake(0.0,200.0,iAd.frame.size.width,iAd.frame.size.height); // does not work
// self.tableView.tableFooterView = iAd; // Display the banner at the middle of the screen (depending upon the number item of the table)
// Do not know how to have the default banner to appear at the very bottom of the screen (behind the tab bar) when the view is loaded ?
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"didFailToReceiveAdWithError");
if (bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
banner.frame = CGRectMake(0.0,350.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"bannerViewDidLoadAd");
if (!bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
banner.frame = CGRectMake(0.0,317.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
bannerIsVisible = YES;
}
}
这个想法是让横幅出现在屏幕底部,并将其显示在选项卡栏上方,即横幅加载成功。
我遇到的问题是:使用这种配置,我可以看到有时会调用方法"didFailToReceiveAdWithError",有时会调用"bannerViewDidLoadAd",但在每种情况下都不会显示横幅。
我知道我来不及回答,但对于那些可能访问此线程寻求答案的人来说。这可能有助于他们
在头文件中
#import <UIKit/UIKit.h>
#import "ListViewController.h"
#import <iAd/iAd.h>
@interface ListViewController : UITableViewController <ADBannerViewDelegate>
@property (nonatomic, strong) ADBannerView *bannerForTableFooter;
@property (nonatomic) BOOL bannershouldBeVisible;
@end
in.m文件
-(ADBannerView *)bannerForTableFooter
{
if (!_bannerForTableFooter) {
_bannerForTableFooter = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerForTableFooter.delegate = self;
}
return _bannerForTableFooter;
}
在视图中DidLoad
self.bannerForTableFooter.backgroundColor = [UIColor clearColor];
然后添加这些tableView数据源方法
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (self.bannershouldBeVisible) {
return self.bannerForTableFooter.frame.size.height;
}
else
{
return 0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (self.bannershouldBeVisible) {
return self.bannerForTableFooter;
}
else
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
}
#pragma mark - iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)bannerShown
{
self.bannershouldBeVisible = YES;
[self.tableView reloadData];
}
-(void)bannerView:(ADBannerView *)bannerShown didFailToReceiveAdWithError:(NSError *)error
{
self.bannershouldBeVisible = NO;
[self.tableView reloadData];
}
您的问题很常见。这不是因为故事板,而是因为你使用的是UITableViewController
。UITableViewControllers
免费给你很多,但它们确实需要一些东西。其中一件"事情"是,他们要求自己的观点是UITableView
。
你有几个选择。这里有几个首选。
1) 将iAd添加为tableFooterView
。这需要一些额外的工作来保持它在屏幕上滚动,就像我对这个问题的回答一样。
2) 将您的UITableViewController
子类转换为UIViewController
子类。有趣的是,上面链接的问题的OP最终就是这么做的。从本质上讲,您可以在为iAd设置动画的同时为tableView(现在是子视图)的帧设置动画。然后它将按预期工作。在这个答案中,我概述了在.xib
文件中转换为UIViewController
子类的基本步骤,其中大部分应该适用于您。
有更好的方法。从这里:https://github.com/romaonthego/RETableViewManager/issues/50
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.iAd.frame;
frame.origin.y = scrollView.contentOffset.y + scrollView.frame.size.height-_iAd.frame.size.height; // Move view to the bottom on scroll
self.iAd.frame = frame;
[scrollView bringSubviewToFront:self.iAd];
}
上升。从iOS7
开始,有更好的方式在底部显示iAd
横幅。
- (void)viewDidLoad
{
[super viewDidLoad];
self.canDisplayBannerAds = YES;
...
}
从iOS 7开始,这些东西都不需要了。正如@Shmidt在他的评论中暗示的那样,你所要做的就是在你的viewDidLoad
中设置self.canDisplayBannerAds = true
,其他一切都会自动处理。这也适用于表视图。