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

时间:2014-05-18 08:48:10

标签: ios ipad admob iad

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

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

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

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

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以通过查看AdMob横幅广告视图中mediatedAdView的尺寸来了解中介广告的尺寸。要垂直居中广告,只需计算出mediatedAdView和横幅视图之间的高度差异,将其减半,然后按该金额调整横幅视图的位置。并且不要忘记在没有调解内容时重新调整位置,否则广告将再次不对齐。

下面的代码示例演示了名为adMobBannerView -

的横幅视图
  1. 首先确定广告的Y位置,假定没有中介内容
  2. 如果有中介内容,则会根据高度的不同计算广告中的空白空间
  3. 然后调整广告的Y位置,以便中介内容垂直居中显示。
  4. 此代码可以进入每次加载新广告时调用的委托处理程序。

    // 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;
    
    祝你好运!

答案 1 :(得分:1)

不幸的是,there doesn't appear to be a standard API to tell you the actual ad height for a given provider。在我的情况下,我使用kGADAdSizeSmartBannerPortrait,无论您是GADBannerView还是iPad,都应该正确调整iPhone的大小。不过,由于iAd only uses 66px in portrait mode on the iPad,但Google Mobile Ads smart banners reserve 90px in portrait mode on the iPad,我的广告底部有空格。

要解决此问题,我使用-[GADBannerView adNetworkClassName]并查找值GADMAdapterIad以确定我是否正在展示iAd。如果是这样,我手动将GADBannerView调整为66px高。