ios - 如何在 iOS 版 Admob 中垂直对齐 iAd View ?

标签 ios ipad admob iad

我什至尝试设置contentMode,Admob的 View 不会垂直对齐广告。

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

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

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

我怎样才能实现这个目标?

最佳答案

您可以通过检查 AdMob 横幅 View 内 mediadAdView 的大小来了解中介广告的大小。要使广告垂直居中,只需算出 mediadAdView 和横幅 View 之间的高度差,将其减半,然后按该高度调整横幅 View 的位置即可。并且不要忘记在没有中介内容时重新调整位置,否则广告将再次失准。

下面的代码示例针对名为 adMobBannerView 的横幅 View 演示了这一点 -

  1. 它首先计算出广告的 Y 位置(假设没有中介内容)
  2. 如果有中介内容,它会根据高度的不同计算广告中的空白空间
  3. 然后,它会调整广告的 Y 位置,使中介内容垂直居中显示。

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

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

祝你好运!

关于ios - 如何在 iOS 版 Admob 中垂直对齐 iAd View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720142/

相关文章:

iOS - 是否可以在一个项目中合并 Swift 3 和 Swift 4 代码?

ios - 我无法让这个 CA 动画正常工作

ios - 滚动后 UICollectionViewCells subview 呈现错误

java - 使用 Android 在 Java 中设置变量的值 24 小时

swift - 优先考虑 iAd 而非 adMob SpriteKit/Swift

ios - 仅在调试变体上启用 UIFileSharingEnabled

iphone - UIImageView setImage 在 iPad 上无法工作,但在 iPhone 上工作正常

ios - 检测 iOS 设备类型

web-applications - 货币化 PWA - 用于通过渐进式 Web 应用程序货币化的方法?

ios - 防止 View 滚动到导航栏