[英]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
的横幅视图 -
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.