AdMob Ad to Landscape Orientation

I checked around the net and found some people looking for this, but nobody who actually posted a code example. So here it is, code to make your Ad Mob ad shift to landscape orientation. My code makes it appear on the bottom, enlarged a little bit, with the device rotated to landscape counter-clockwise.

Add in a UIView called adContainer in your delegate interface declaration, and then update the standard Ad Mob methods as follows.

- (void)showAd 
{	
	adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
	[adMobAd retain]; // this will be released when it loads (or fails to load)
	adContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 432, 320, 48)];
}
#define degreesToRadians(x) (M_PI * x / 180.0)
 
- (void)didReceiveAd:(AdMobView *)adView 
{
	[adContainer addSubview: adMobAd];
	[window addSubview:adContainer];
	CGAffineTransform makeLandscape = CGAffineTransformMakeRotation(degreesToRadians(90));
	makeLandscape = CGAffineTransformTranslate(makeLandscape, -480/2 + 48/2, 320/2 - 48/2 - 12);
	makeLandscape = CGAffineTransformScale(makeLandscape, 480.0/320, 480.0/320);
	adContainer.transform = makeLandscape;
	autoslider = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}
- (void)removeAd 
{
	[adMobAd removeFromSuperview];
	[adMobAd release];
	adMobAd = nil;
 
	[adContainer removeFromSuperview];
	[adContainer release];
	adContainer = nil;
}

This was actually my first experience using the CG transforms, so please be gentle if there’s a better way :) .

  1. James Dailey says:

    Here is an alternate solution I found. Just copy ProgrammaticAdViewController.m and .h from AdMob examples into your project then add the lines below in your appDelegate. This puts admob in top left corner and it’s scaled down a tiny bit.

    UINavigationController *navigationController;
    [window addSubview:[navigationController view]];

    AdMobView *ad = [AdMobView requestAdWithDelegate:[[ProgrammaticAdViewController alloc] init]];

    ad.frame = CGRectMake(0, 432, 320, 48);
    [self.window addSubview:ad];

    CGAffineTransform makeLandscape = CGAffineTransformMakeRotation(M_PI * 0.5f);

    makeLandscape = CGAffineTransformTranslate(makeLandscape, -103, -144);

    makeLandscape = CGAffineTransformScale(makeLandscape, 0.8f, 0.8f);

    ad.transform = makeLandscape;

  2. James Dailey says:

    Oops. Top right corner.

  3. jon giles says:

    when i use this code, I’m not getting a response for tapping my ad… anyone know what im missing?

  1. There are no trackbacks for this post yet.

Leave a Reply