使用此代码似乎无法将 iAd 横幅移动到屏幕顶部
Cannot seem to move iAd banner to the top of the screen with this code
我搜索了又搜索,并在尝试将广告横幅移动到屏幕顶部的代码上进行了大量试验和错误。已经一周了,毫无进展。如果有人能帮我解决这个问题,我会很高兴。这是基于的代码来自这位绅士的精彩教程:
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
更改此行:
_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 坐标(从第一个中删除 (-) 并将其添加到第二个中),因为您目前拥有的那些假设框架是在屏幕底部。
希望对您有所帮助!
我搜索了又搜索,并在尝试将广告横幅移动到屏幕顶部的代码上进行了大量试验和错误。已经一周了,毫无进展。如果有人能帮我解决这个问题,我会很高兴。这是基于的代码来自这位绅士的精彩教程: 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
更改此行:
_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 坐标(从第一个中删除 (-) 并将其添加到第二个中),因为您目前拥有的那些假设框架是在屏幕底部。
希望对您有所帮助!