似乎无法使用此代码将iAd横幅移动到屏幕顶部

时间:2015-01-03 21:03:52

标签: sprite-kit iad adbannerview banner-ads

我已搜索并搜索过这段代码并尝试将广告横幅移至屏幕顶部。已经过了一个星期,没有任何进展。如果有人能帮我解决这个问题,我会很激动。这是基于绅士的伟大教程的代码: 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

1 个答案:

答案 0 :(得分: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坐标(从第一个中删除( - )并将其添加到第二个),因为你现在拥有的坐标是在屏幕的底部。

希望这有帮助!