behind the aura
behind the aura

Reputation: 296

Admob banner change frame for different screen dimensions

I'm trying to get my Admob ad to adjust between all the different iPhone screen sizes that Apple offers, but I can't figure out how to get it to adjust automatically or by using different code.

In my viewdidload

bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
bannerView_.adUnitID = @"";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:@"", nil ];

Upvotes: 2

Views: 1367

Answers (2)

Daniel Storm
Daniel Storm

Reputation: 18878

You need to set the adSize property to Google's set banner size kGADAdSizeBanner. Then you set your GADBannerView's frame relative to the view. The following code places the GADBannerView on the bottom of your view.

admobBannerView.adSize = kGADAdSizeBanner;
admobBannerView.frame = CGRectMake(0, self.view.frame.size.height - admobBannerView.frame.size.height , self.view.frame.size.width, admobBannerView.frame.size.height);
  • x - Set to origin of 0
  • y - Set origin to the view's height. But this will make it appear off the bottom of the screen. So, we must subtract the height of our GADBannerView to move it up onto the screen: self.view.frame.size.height - admobBannerView.frame.size.height
  • width - Set it to the width of the view: self.view.frame.size.width
  • height - Set it to the height of our GADBannerView: admobBannerView.frame.size.height

Banner Ad Customization

Upvotes: 3

dstudeba
dstudeba

Reputation: 9038

Since you are creating the banner programmatically instead of with Storyboard, I would check to find out what the height of the screen is. I use the following code to determine what phone someone is on.

CGFloat greaterPixelDimension = (CGFloat) fmaxf(((float)[[UIScreen mainScreen]bounds].size.height), ((float)[[UIScreen mainScreen]bounds].size.width));

I would then create the bannerview in the code like this:

if (greaterPixelDimension == 736) {  //6 plus
    bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 4--, 50)];
}
else if (greaterPixelDimension == 667){  // 6
     bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, ###, 3--, 50)];
}
else if(greaterPixelDimension == 568){  // 5
     bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
}

etc. (obviously replace the ### and 3-- with the respective points, I forget the numbers off the top of my head)

Upvotes: 0

Related Questions