简体   繁体   中英

How to vertically align iAd view in Admob for iOS?

I tried even setting contentMode , Admob's view does not vertically align advertisements.

For example, I add the advertisement view into the view hierarchy and give it 90px height which is the same height for the Admob's size constant. So, when Admob's own ads are loaded, it fills the space perfectly.

On the other hand, as iAd's height is 66px by documentation. it shows at the top of the Admob view.. not centering it vertically. So there are 24px empty space below that iAd banner.

I couldn't find a safe way to vertically align everything in a given height.(90px)

How can I achieve this?

You can find out the size of the mediated ad by checking the size of mediatedAdView inside the AdMob banner view. To vertically center the ad, just work out the difference in height between the mediatedAdView and the banner view, halve it, and adjust the position of the banner view by that amount. And don't forget to readjust the position when there is no mediated content, otherwise the ad will again be out of alignment.

The code sample below demonstrates this for a banner view called adMobBannerView -

  1. It first works out the Y position of the ad, assuming no mediated content
  2. If there is mediated content, it calculates how much empty space is in the ad, based on the different in heights
  3. It then adjusts the Y position of the ad, so that the mediated content appears vertically centered.

This code could go into the delegate handler that is called each time a new ad is loaded.

// What is the normal vertical position of the ad? For this example, the vertical position is sitting at the bottom of the screen.
int adViewNormalY = self.view.bounds.size.height - adMobBannerView.frame.size.height;

// By default, assume there is no empty space in the ad
int emptySpaceAtBottomOfAd = 0;

// Check that this is a mediated ad, not a normal AdMob ad 
if (adMobBannerView.mediatedAdView)
{
   // Subtract the height of the mediated ad from the ad view
   emptySpaceAtBottomOfAd = adMobBannerView.frame.size.height - adMobBannerView.mediatedAdView.frame.size.height;
}

// Move the ad view down so that the mediated ad appears centered
CGRect newFrame = adMobBannerView.frame
newFrame.origin.y = adViewNormalY + (emptySpaceAtBottomOfAd / 2);        
adMobBannerView.frame = newFrame;

Good luck!

Unfortunately, there doesn't appear to be a standard API to tell you the actual ad height for a given provider . In my case, I used kGADAdSizeSmartBannerPortrait , which is supposed to size the GADBannerView correctly regardless of whether you're on iPad or iPhone . However, since iAd only uses 66px in portrait mode on the iPad , but Google Mobile Ads smart banners reserve 90px in portrait mode on the iPad , I had empty space at the bottom of my ad.

To fix this problem, I used -[GADBannerView adNetworkClassName] and looked for a value of GADMAdapterIad to determine whether I was showing an iAd. If so, I manually resized the GADBannerView to be 66px tall.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM