我试图在我的应用程序底部放置一个adbanner(admob横幅)。但是目前通过使用我的代码,它有时出现在底部或中间。请检查以下屏幕截图
请检查我的代码
#import "LocViewController.h"
@interface LocViewController ()
{
UIInterfaceOrientation orientation1;
}
@end
#define MY_BANNER_UNIT_ID @"a152a98e7f64210"
@implementation LocViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Create a view of the standard size at the top of the screen.
// Available AdSize constants are explained in GADAdSize.h.
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//Do what you want in Landscape Left
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
bannerView_.frame = CGRectMake(0,678,bannerView_.frame.size.width,bannerView_.frame.size.height);
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do what you want in Portrait
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
bannerView_.frame = CGRectMake(0,934,bannerView_.frame.size.width,bannerView_.frame.size.height);
}
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];
}
-(void)showAd:(NSString *)orient{
// Create a view of the standard size at the top of the screen.
// Available AdSize constants are explained in GADAdSize.h.
if([orient isEqualToString:@"l"]){
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
bannerView_.frame = CGRectMake(0,678,bannerView_.frame.size.width,bannerView_.frame.size.height);
}else{
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
bannerView_.frame = CGRectMake(0,934,bannerView_.frame.size.width,bannerView_.frame.size.height);
}
// Specify the ad unit ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];
}
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
[bannerView_ removeFromSuperview];
if(orientation == UIInterfaceOrientationPortrait||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//load the portrait view
[self showAd:@"p"];
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//load the landscape view
[self showAd:@"l"];
}
}
-(void)viewDidDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}