簡體   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