1

我已经搜索和搜索,并在此代码上进行了大量试验和错误,试图将广告横幅移动到屏幕顶部。已经一个星期了,一点进展都没有。如果有人可以帮助我解决这个问题,我会很高兴。这是基于的代码是由绅士的伟大教程: http ://codewithchris.com/iad-tutorial/

这是代码:

@interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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

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

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (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)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"Failed to retrieve ad");

if (_bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];

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

        [UIView commitAnimations];

        _bannerIsVisible = NO;
    }
}

@end
4

1 回答 1

0

更改此行:

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

对此:

_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];

(其中 -50 是广告横幅的高度。)在使用 self.view.frame.size.height 将广告横幅的框架设置到屏幕底部之前,而现在 -50 将框架移动到屏幕。

另外,不要忘记您需要反转 CGRectOffsets 的 y 坐标(从第一个删除 (-) 并将其添加到第二个),因为您目前拥有的那些假设框架位于底部屏幕。

希望这可以帮助!

于 2015-01-03T23:27:02.560 回答