【问题标题】:Dark bars on the top and bottom of banner ad in IOS appIOS 应用中横幅广告顶部和底部的黑条
【发布时间】:2020-07-27 21:43:03
【问题描述】:

我刚刚在 IOS 应用程序中集成了横幅广告。我使用了一个名为“adContainerView”的容器视图,当广告准备就绪时,我将 GADBannerView 实例添加到 adContainerView。

用于加载横幅广告的代码 sn-p 是

var bannerView = GADBannerView(adSize: kGADAdSizeBanner)

func createAndLoadBannerAd() {
        bannerView.adUnitID = "ca-app-pub-xxxxxxxxxxxx"
        bannerView.rootViewController = self
        bannerView.delegate = self
        bannerView.load(GADRequest())
    }

当广告在下面加载时,事件被调用,我将 adContainerView 视图的高度和宽度设置为广告尺寸。

func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        adViewHeightConstraint.constant = bannerView.adSize.size.height
        adViewWidthConstraint.constant = bannerView.adSize.size.width
        
         bannerView.translatesAutoresizingMaskIntoConstraints = false
         adContainerView.addSubview(bannerView)
         NSLayoutConstraint.activate([
             bannerView.leadingAnchor.constraint(equalTo: adContainerView.leadingAnchor),
             bannerView.trailingAnchor.constraint(equalTo: adContainerView.trailingAnchor),
             bannerView.topAnchor.constraint(equalTo: adContainerView.topAnchor),
             bannerView.bottomAnchor.constraint(equalTo: adContainerView.bottomAnchor)
         ])
    }

但是,广告有时会奇怪地出现。它有时如下所示,这是我所期待的

当我来回访问同一页面时,随机显示如下。

我想了解为什么我会在广告视图的顶部和底部看到黑条。 adContainerView 采用 GDBannerView 的确切大小,它是 adContainerView 的子级。因此,从技术上讲,它不应该在广告的顶部和底部显示暗条。

关于为什么会发生这种情况的任何指示。

【问题讨论】:

    标签: ios swift googlemobileads


    【解决方案1】:

    BannerView 可以直接添加到主视图中,而不是添加另一个 adContainerView 以适应bannerView 的大小

    而且最好在 func createAndLoadBannerAd() 中添加任何约束,然后再放在bannerView.load(GADRequest())

    var bannerView = GADBannerView(adSize: kGADAdSizeBanner)
    
    func createAndLoadBannerAd() {
        bannerView.adUnitID = "ca-app-pub-xxxxxxxxxxxx"
        bannerView.rootViewController = self
        bannerView.delegate = self
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        mainView.addSubview(bannerView)
        bannerView.bottomAnchor.constraint(equalTo: mainView.bottomAnchor).isActive = true
        bannerView.centerXAnchor.constraint(equalTo: mainView.centerXAnchor).isActive = true
        bannerView.load(GADRequest())
    }
    

    上面的代码会将bannerView放在mainView的底部。

    编辑:我使用了 320x100 大横幅 kGADAdSizeLargeBanner 并按上述方式实施,我的广告图片也具有相同的尺寸,到目前为止我在实时版本中没有遇到任何此类问题。在测试模式下,我也不记得遇到过这样的问题。

    【讨论】:

    • 试过这个,但也以同样的随机行为结束。在我的例子中,adContainerView 最初的宽度和高度约束为零。我拥有容器视图的唯一原因是确保 adContainerview 获得大小并仅在广告加载成功时才显示广告,否则使用真实状态来显示应用内容。
    • 广告横幅的尺寸是多少?希望在您使用 kGADAdSizeBanner 时正好是 320x50。
    • 我切换到 kGADAdSizeLargeBanner 并没有发现任何问题。该问题仅发生在 kGADAdSizeBanner 中。奇怪。
    • 很可能,从 Google 测试服务器发送的广告图片在广告图片中已经存在这些顶部和底部的黑条。如果您获得 320x50,请尝试打印横幅的高度和宽度,然后无需担心并继续。
    • 是的。横幅高度和宽度为 50 * 320。
    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 2016-12-06
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2014-09-24
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多