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



我甚至尝试设置contentMode, Admob的视图不垂直对齐广告。

例如,我将广告视图添加到视图层次结构中,并赋予它90px的高度,这与Admob的大小常数的高度相同。因此,当Admob自己的广告被加载时,它会完美地填充空间。

另一方面,作为iAd的高度是66px的文档。它显示在Admob视图的顶部。不是垂直居中。所以在iAd广告条下面有24px的空白

我找不到一种安全的方法来垂直对齐给定高度的所有内容。(90px)

我怎样才能做到这一点?

您可以通过查看AdMob横幅视图内mediatedAdView的大小来查找中介广告的大小。要使广告垂直居中,只需计算出mediatedAdView与横幅视图之间的高度差,将其减半,并根据该量调整横幅视图的位置。当没有媒介内容时,不要忘记重新调整位置,否则广告会再次错位。

下面的代码示例为名为adMobBannerView - 的横幅视图演示了这一点
  1. 它首先计算出广告的Y位置,假设没有中介内容
  2. 如果有中介内容,它会根据高度的不同来计算广告中有多少空白空间
  3. 然后调整广告的Y轴位置,使中介内容垂直居中显示。

此代码可以放入每次加载新广告时调用的委托处理程序中。

// 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;

祝你好运!

不幸的是,似乎没有一个标准的API来告诉您给定提供商的实际广告高度。在我的情况下,我使用kGADAdSizeSmartBannerPortrait,它应该正确地调整GADBannerView的大小,无论您是在iPad还是iPhone上。但是,由于iAd在iPad上的纵向模式下只使用66px,而Google Mobile Ads智能横幅在iPad上的纵向模式下保留90px,所以我在广告底部设置了空白空间。

为了解决这个问题,我使用-[GADBannerView adNetworkClassName]并查找GADMAdapterIad的值来确定我是否显示iAd。如果是这样,我手动调整GADBannerView的大小为66px高。

最新更新