繁体   English   中英

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

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

我甚至试过设置contentMode ,Admob的视图没有垂直对齐广告。

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

另一方面,由于文档的iAd高度为66px。 它显示在Admob视图的顶部..不垂直居中。 因此iAd横幅下方有24px空白区域。

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

我怎样才能做到这一点?

您可以通过检查的大小找出中介广告的大小mediatedAdView AdMob广告横幅广告视图中。 要将广告垂直居中,只需计算出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 ,无论你是在iPad还是iPhone上,都应该正确调整GADBannerView大小。 但是,由于iAd仅在iPad上以纵向模式使用66px ,但Google Mobile Ads智能横幅在iPad上以纵向模式保留90px ,因此我的广告底部有空白空间。

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

暂无
暂无

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

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