如何正确地将 iAd 横幅放置在选项卡控制器上?

How to correctly place an iAd Banner over a Tab Controller?

我正在尝试在屏幕底部上方 50pts 的 TabBar 顶部添加一个 iAd 横幅,但由于某种原因,横幅每次都在屏幕上向上移动 50pts,在它之后刷新。

我在 tabBarViewController 中这样初始化它:

- (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);。每次从 iAd 网络收到新广告时,它都会将您的 ADBannerView 的 y 位置偏移 50pts。我假设在你的 bannerView:didFailToReceiveAdWithError: 中你没有正确设置你的 _bannerIsVisible,据我所知应该是 _bannerIsVisible = NO。此外,您应该在 viewDidLoad 中创建一次 ADBannerView,而不是在 viewDidAppear 中创建一次,因为这很可能会在您的应用程序会话中多次调用。

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

这是我所建议的示例。我已经注释掉了大部分。如果您需要进一步说明,请告诉我。

#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 文档:ADBannerViewDelegate, CGRectOffset, viewDidAppear, frame