以前,写了一篇文章:将AdMob加入iOS应用中,这篇文章中的方法只支持竖屏下的AdMob展示,而此文支持横屏和竖屏,AdMob会自动切换。
AdMobViewController.h
#import <UIKit/UIKit.h> #import "GADBannerView.h" @interface aaViewController : UIViewController { GADBannerView *banner; } @end
AdMobViewController.m
- (void)viewDidLoad { [super viewDidLoad]; CGPoint origin = CGPointMake(0, self.view.frame.size.height - CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height); banner = [[[GADBannerView alloc]initWithAdSize:kGADAdSizeSmartBannerPortrait origin:origin] autorelease]; banner.adUnitID = @""; banner.rootViewController = self; [self.view addSubview:banner]; GADRequest *request = [GADRequest request]; request.additionalParameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"AAAAFF", @"color_bg", @"FFFFFF", @"color_bg_top", @"FFFFFF", @"color_border", @"000080", @"color_link", @"808080", @"color_text", @"008000", @"color_url", nil]; [banner loadRequest:request]; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { CGFloat y; if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){ banner.adSize = kGADAdSizeSmartBannerLandscape; y = self.view.frame.size.width - CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).height; } else { banner.adSize = kGADAdSizeSmartBannerPortrait; y = self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height; } CGRect frame = banner.frame; frame.origin = CGPointMake(0, y); banner.frame = frame; }
效果图: