我正在将iAd和AdMob横幅广告应用到我的应用中。在iPad上,当设备旋转时,我会遇到一些奇怪的问题,特别是AdMob。
使用iAds时,当设备旋转并且不重新加载广告时,横幅仍保留在屏幕底部。
然而,使用AdMob,即使我使用相同的代码,它也会在设备旋转时重新加载横幅。
我正在以编程方式创建ADBannerView
和GADBannerView
。
iAd代码:
self.adBanner.hidden = NO;
self.adBanner = [[self appdelegate] adBanners];
self.adBanner.delegate = self;
if (IDIOM == IPAD)
{
NSLog(@"***This is the iPad****");
[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adBanner setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adBanner];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adBanner
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
AdMob代码如下。我在applicationDidFinishLaunchingWithOptions中的AppDelegate中创建了GADBannerView:
更新
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// other code
self.adBanners = [[ADBannerView alloc]init];
self.adBanners.hidden = YES;
self.adMobBanners = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
return YES;
}
在View Controller中,当我创建AdMob时,我正在调用创建AdMob的方法:
更新
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"View will appear and the IAP is not Successful");
[self sharedBanners];
}
else
{
NSLog(@"View will appear and the IAP IS Successful");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}
- (void)sharedBanners
{
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
self.adBanner = [[self appdelegate] adBanners];
self.adBanner.delegate = self;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self displayiAdsOrNot];
}
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
[self displayAdMobBannerOrNot];
}
- (CustomAppDelegate *)appdelegate
{
return (CustomAppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void)displayAdMobBannerOrNot
{
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = NO;
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
if (IDIOM == IPAD) {
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
self.adMobBannerView.adUnitID = @"MYUNIT";
// [self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeFullBanner).width, 50)];
GADRequest *request = [GADRequest request];
[self.adMobBannerView loadRequest:request];
[self.adMobBannerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adMobBannerView];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
}
**请注意,这不是方法的顺序。实际的顺序是AppDelegate,sharedBanner,displayAdMob,viewWillAppear,然后是委托方法。 **
限制的原因是我希望将ADBannerView
和GADBannerView
固定在屏幕底部并拖尾和向左移动。我的意思是,我希望它从屏幕底部开始,从左边缘开始,在右边缘和底部结束。
问题
当iAd横幅加载时,它会在iPad屏幕的整个底部工作,从左侧开始,在右侧结束。如果我旋转设备,iAd横幅不会重新加载,它会继续随iPad一起旋转。但是,AdMob横幅以纵向模式显示,但是当我旋转时,它会消失,然后重新加载。
我尝试使用Banner Ad Customization作为常量而不是AdMob横幅的显式尺寸。例如:
if (UIInterfaceOrientationLandscapeLeft)
{
NSLog(@"Left");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).width, 90)];
}
else if (UIInterfaceOrientationLandscapeRight)
{
NSLog(@"Right");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).width, 90)];
}
else if (UIInterfaceOrientationPortrait)
{
NSLog(@"Portraait");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).width, 90)];
}
但问题仍然存在。