xcode 使用故事板在 UItableView 中添加 iAd 横幅
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11400876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add iAd banner in UItableView with storyboard
提问by Luc
I do not manage to have iAd working in storyboard, within a UITableViewController.
我没有设法让 iAd 在故事板中工作,在 UITableViewController 中。
This is basically what I have done so far: In storyboard, I have dragged an iAd banner view in the bottom of the scene of my UITableViewController. (I was not able to set the banner in the view itself as storyboard prevents it. I could only drag the iAd object next to the first responder and UITableViewController's icons.)
到目前为止,这基本上就是我所做的:在故事板中,我在 UITableViewController 的场景底部拖动了一个 iAd 横幅视图。(我无法在视图本身中设置横幅,因为故事板阻止了它。我只能拖动第一响应者和 UITableViewController 图标旁边的 iAd 对象。)
In the header of the UITAbleViewController, I have:
在 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
In the implementation of the UITableViewController, I have:
在 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;
}
}
The idea is to have the banner appearing at the bottom of the screen and to show it above the tab bar is the banner load is succesfulf.
这个想法是让横幅出现在屏幕底部,并在标签栏上方显示横幅加载成功。
The problem I have: with this configuration I can see that sometimes the method "didFailToReceiveAdWithError" is called and sometimes the "bannerViewDidLoadAd" one, but in each case the banner is not shown.
我遇到的问题:通过这种配置,我可以看到有时调用方法“didFailToReceiveAdWithError”,有时调用“bannerViewDidLoadAd”方法,但在每种情况下都没有显示横幅。
回答by Taseen
I know I am too late for the answer but for people who might visit this thread for answer. this might help them
我知道我给出答案为时已晚,但对于可能访问此线程寻求答案的人来说。这可能会帮助他们
in header file
在头文件中
#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 file
在 .m 文件中
-(ADBannerView *)bannerForTableFooter
{
if (!_bannerForTableFooter) {
_bannerForTableFooter = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerForTableFooter.delegate = self;
}
return _bannerForTableFooter;
}
in ViewDidLoad
在 ViewDidLoad
self.bannerForTableFooter.backgroundColor = [UIColor clearColor];
and then add these tableView datasource methods
然后添加这些 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];
}
回答by NJones
Your problem is a common one. It's not happening due to storyboard but rather is happening due to the fact the you are using a UITableViewController
. UITableViewControllers
give you a lot for free, but they do require a few things. One of those 'things' is that they demand their view be a UITableView
.
你的问题很常见。这不是由于情节提要而发生,而是由于您使用UITableViewController
. UITableViewControllers
免费给你很多,但它们确实需要一些东西。其中一个“事情”是他们要求他们的观点是UITableView
.
You have a few options. Here are a couple of the preferred ones.
你有几个选择。这里有几个首选的。
1) Add your iAd as the tableFooterView
. This requires some extra work to keep it onscreen while scrolling as out lined in my answer to this question.
1) 将您的 iAd 添加为tableFooterView
. 这需要一些额外的工作才能在滚动时将其保留在屏幕上,如我对此问题的回答中所述。
2) Convert your UITableViewController
subclass to be a UIViewController
subclass. This is interestingly what the OP of the question linked above ended up doing. Essentially then you can animate the (now a subview) tableView's frame while animating the iAd. And then it will work as expected. I outlined the basic steps for converting to a UIViewController
subclass in a .xib
file in this answer, most of it should be applicable to you.
2)将您的UITableViewController
子类转换为UIViewController
子类。有趣的是,上面链接的问题的 OP 最终做了什么。基本上,您可以在为 iAd 设置动画时为(现在是子视图)tableView 的框架设置动画。然后它将按预期工作。我在这个答案中概述了转换为文件中的UIViewController
子类的基本步骤.xib
,其中大部分应该适用于您。
回答by Shmidt
There is better way. From here: https://github.com/romaonthego/RETableViewManager/issues/50
有更好的办法。从这里: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];
}
Upd. There is better way to show iAd
banner at the bottom since iOS7
.
更新。有更好的方式来显示iAd
在自下横幅iOS7
。
- (void)viewDidLoad
{
[super viewDidLoad];
self.canDisplayBannerAds = YES;
...
}
回答by LNI
From iOS 7 onwards none of these things are needed. All you have to do, as @Shmidt implied in his comment, is set self.canDisplayBannerAds = true
in your viewDidLoad
and everything else is automatically handled. This works beautifully with Table Views too.
从 iOS 7 开始,这些东西都不需要了。正如@Shmidt 在他的评论中暗示的那样,您所要做的一切都self.canDisplayBannerAds = true
在您的设置中viewDidLoad
,其他一切都会自动处理。这也适用于表格视图。