如何在标签控制器上正确放置iAd标语? [英] How to correctly place an iAd Banner over a Tab Controller?

查看:65
本文介绍了如何在标签控制器上正确放置iAd标语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在屏幕底部上方50pts的TabBar顶部添加iAd横幅广告,但是由于某种原因,横幅广告刷新后,横幅广告每次在屏幕上向上移动50pts.

I am trying to add an iAd banner on top of a TabBar, which is 50pts above the bottom of the screen, but for some reason, the Banner moves up, 50pts, across the screen, every time, after it refreshes.

我通过以下方式在tabBarViewController中对其进行初始化:

I'm initializing it in the tabBarViewController this way:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!_bannerIsVisible)
    {
        // If banner isn't part of view hierarchy, add it
        if (_adBanner.superview == nil)
        {
            [self.view addSubview:_adBanner];
        }

        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];

        // Assumes the banner view is just off the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);

        [UIView commitAnimations];

        _bannerIsVisible = YES;
    }
}

我将其放置为这样:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    _adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];

    _adBanner.delegate = self;
}

为什么会这样?

推荐答案

您使用的代码是实现ADBannerView的一种很旧的方法.您的ADBannerView在屏幕上垂直移动的原因是由于该行banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);.每当ADBannerView的y位置从iAd网络接收到新广告时,它就会偏移50pts.我假设在您的bannerView:didFailToReceiveAdWithError:中,您没有正确设置_bannerIsVisible,应该是我可以收集到的_bannerIsVisible = NO.另外,您应该在viewDidLoad中创建一次ADBannerView,而不是在viewDidAppear中创建,因为这很可能在您的应用会话中被多次调用.

The code you are using is quite an old way of implementing an ADBannerView. The reason your ADBannerView is moving vertically across the screen is because of this line banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);. It is offsetting your ADBannerView's y position by 50pts each time it receives a new ad from the iAd network. I'm assuming that in your bannerView:didFailToReceiveAdWithError: you are not setting your _bannerIsVisible properly, should be _bannerIsVisible = NO from what I can gather. Also, you should be creating your ADBannerView once in your viewDidLoad, not in your viewDidAppear as this will most likely be called more than once in your app session.

或者,您可以根据UITabBar的位置来设置ADBannerView的位置.然后,当您没有收到来自iAd的广告时,可以将其设置为view动画或完全隐藏ADBannerView.您应该根据ADBannerView的尺寸来设置ADBannerView的尺寸.由于现在有各种不同的屏幕尺寸可用,并且将来还会有更多屏幕尺寸,以这种方式进行设置可以确保在引入新设备时ADBannerView继续以最少的维护工作.

Alternatively, you could set your ADBannerView's position based on the position of your UITabBar. Then, when you do not receive an ad from iAd you could either animate it off the view or hide the ADBannerView completely. You should be setting your ADBannerView's dimensions based on the size of the view it is on also. With all the different screen sizes available now, and more to come in the future, setting things up in this way will assure your ADBannerView continues to work with minimal maintenance when new devices are introduced.

这是我所建议的一个例子.我已经注释掉了大部分.让我知道您是否需要进一步的澄清.

Here's an example of what I am suggesting. I've commented out most of it. Let me know if you need any further clarification.

#import "ViewController.h"
@import iAd; // Import iAd

@interface ViewController () <ADBannerViewDelegate> // Include delegate
// Outlet to a UITabBar I created in Interface Builder
@property (weak, nonatomic) IBOutlet UITabBar *myTabBar;
@end

@implementation ViewController {
    ADBannerView *iAdBannerView;
}

-(void)viewDidLoad {
    [super viewDidLoad];

    iAdBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    iAdBannerView.frame = CGRectMake(0, // x = 0
                                     self.view.frame.size.height - self.myTabBar.frame.size.height - iAdBannerView.frame.size.height,
                                     // y = get the height of our view, subtract the height of our UITabBar, subtract the height of our ADBannerView
                                     self.view.frame.size.width, // width = stretch our ADBannerView across the width of our view
                                     iAdBannerView.frame.size.height); // height = height of our ADBannerView
    iAdBannerView.alpha = 0.0; // Hide our ADBannerView initially because it takes a second to receive an ad
    iAdBannerView.delegate = self; // Set its delegate
    [self.view addSubview:iAdBannerView]; // Add it to our view
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"bannerViewDidLoadAd");

    // Fade in our ADBannerView
    [ADBannerView animateWithDuration:0.2
                            delay:0
                          options:0
                       animations:^{
                           iAdBannerView.alpha = 1.0;
                       }
                       completion:^(BOOL finished) {
                           if (finished) {
                               // If you wanted to do anything once the animation finishes
                           }
                       }];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"didFailToReceiveAdWithError: %@", error);

    // Fade out our ADBannerView
    [ADBannerView animateWithDuration:0.2
                            delay:0
                          options:0
                       animations:^{
                           iAdBannerView.alpha = 0.0;
                       }
                       completion:^(BOOL finished) {
                           if (finished) {
                           }
                       }];
}

您还应该熟悉以下Apple文档:

You should familiarize yourself with these Apple Docs also: ADBannerViewDelegate, CGRectOffset, viewDidAppear, frame.

这篇关于如何在标签控制器上正确放置iAd标语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆