简体   繁体   English

如何在Admob for iOS中垂直对齐iAd视图?

[英]How to vertically align iAd view in Admob for iOS?

I tried even setting contentMode , Admob's view does not vertically align advertisements. 我甚至试过设置contentMode ,Admob的视图没有垂直对齐广告。

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. 例如,我将广告视图添加到视图层次结构中,并将其赋予90px高度,这与Admob的大小常量相同。 So, when Admob's own ads are loaded, it fills the space perfectly. 因此,当Admob自己的广告加载时,它会完美填补空间。

On the other hand, as iAd's height is 66px by documentation. 另一方面,由于文档的iAd高度为66px。 it shows at the top of the Admob view.. not centering it vertically. 它显示在Admob视图的顶部..不垂直居中。 So there are 24px empty space below that iAd banner. 因此iAd横幅下方有24px空白区域。

I couldn't find a safe way to vertically align everything in a given height.(90px) 我无法找到一种安全的方法来垂直对齐给定高度的所有东西。(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. 您可以通过检查的大小找出中介广告的大小mediatedAdView AdMob广告横幅广告视图中。 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. 要将广告垂直居中,只需计算出mediatedAdView与横幅视图之间的高度差异,将其减半,然后按该数量调整横幅视图的位置。 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 - 下面的代码示例演示了一个名为adMobBannerView的横幅视图 -

  1. It first works out the Y position of the ad, assuming no mediated content 它首先计算出广告的Y位置,假设没有中介内容
  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. 然后,它会调整广告的Y位置,以便中介内容垂直居中显示。

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 . 不幸的是, 似乎没有标准的API来告诉您给定提供商的实际广告高度 In my case, I used kGADAdSizeSmartBannerPortrait , which is supposed to size the GADBannerView correctly regardless of whether you're on iPad or iPhone . 在我的例子中,我使用了kGADAdSizeSmartBannerPortrait ,无论你是在iPad还是iPhone上,都应该正确调整GADBannerView大小。 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. 但是,由于iAd仅在iPad上以纵向模式使用66px ,但Google Mobile Ads智能横幅在iPad上以纵向模式保留90px ,因此我的广告底部有空白空间。

To fix this problem, I used -[GADBannerView adNetworkClassName] and looked for a value of GADMAdapterIad to determine whether I was showing an iAd. 为了解决这个问题,我使用了-[GADBannerView adNetworkClassName]并查找了GADMAdapterIad的值来确定我是否正在显示iAd。 If so, I manually resized the GADBannerView to be 66px tall. 如果是这样,我手动调整GADBannerView大小为66px高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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