如何正确地将一个横幅广告放置在Tab控制器上?

我试图在TabBar顶部添加一个iAd横幅,该横幅在屏幕底部以上50pts,但由于某种原因,横幅在屏幕刷新后每次都会向上移动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); 。 每次从iAdnetworking收到新的广告时, ADBannerView的sy位置就会被抵消ADBannerView 。 我假设在你的bannerView:didFailToReceiveAdWithError:你没有正确设置你的_bannerIsVisible ,应该是_bannerIsVisible = NO从我可以收集。 另外,你应该在你的viewDidLoad创build一次你的ADBannerView ,而不是在你的viewDidAppear因为这很可能会在你的应用程序会话中多次调用。

或者,您可以根据您的UITabBar的位置来设置您的ADBannerView的位置。 然后,当您没有收到来自iAd的广告时,您可以将其从viewADBannerView或完全隐藏ADBannerView 。 您应该根据view的大小来设置您的ADBannerView的尺寸。 随着所有不同的屏幕尺寸现在可用,以及将来会有更多的屏幕尺寸,以这种方式进行设置将确保您的ADBannerView在引入新设备时以最less的维护工作继续工作。

这是我所build议的一个例子。 我已经评论了大部分。 让我知道你是否需要进一步澄清。

 #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 Docs: ADBannerViewDelegate , CGRectOffset , viewDidAppear , frame 。