【问题标题】:iOS: Admob smart banner does not taking full widthiOS:Admob 智能横幅不占用全宽
【发布时间】:2017-08-23 18:01:10
【问题描述】:

我创建了横幅:

    bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)  
    bannerView.adUnitID = "ca-76543456707"
    bannerView.delegate = self

它应该是全宽的,我将它添加到带有前导、拖曳、顶部和底部约束的视图中。对于大多数广告,结果是全宽的。但对有些人来说不是。为什么? 这是一个示例: 这里的红色是视图颜色。

如何让它全宽?

【问题讨论】:

  • 对我来说它甚至不是中心

标签: ios uiviewcontroller admob uiwindow adbannerview


【解决方案1】:

通常,手机上的智能横幅纵向高度为 50dp 和 32dp 横向。在平板电脑上,高度通常为 90dp 方向。

当图片广告的大小不足以占据整个分配的 空间,图像将居中,两侧的空间将 填写。

您也可以在 AppStore 上查看实时应用,您会注意到在较大宽度的设备上它们也显示 320 宽度。

【讨论】:

    【解决方案2】:
     func addBannerViewToView(_ bannerView: GADBannerView) {
       bannerView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(bannerView)  
    
      if #available(iOS 11.0, *) {
          // In iOS 11, we need to constrain the view to the safe area.
          positionBannerViewFullWidthAtBottomOfSafeArea(bannerView)
        }
        else {
          // In lower iOS versions, safe area is not available so we use
          // bottom layout guide and view edges.
          positionBannerViewFullWidthAtBottomOfView(bannerView)
        }
    }
    
    
    
        func positionBannerViewFullWidthAtBottomOfSafeArea(_ bannerView: UIView) {
          // Position the banner. Stick it to the bottom of the Safe Area.
          // Make it constrained to the edges of the safe area.
          let guide = view.safeAreaLayoutGuide
          NSLayoutConstraint.activate([
            guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor),
            guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor),
            guide.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor)
          ])
        }
    
        func positionBannerViewFullWidthAtBottomOfView(_ bannerView: UIView) {
          view.addConstraint(NSLayoutConstraint(item: bannerView,
                                                attribute: .leading,
                                                relatedBy: .equal,
                                                toItem: view,
                                                attribute: .leading,
                                                multiplier: 1,
                                                constant: 0))
          view.addConstraint(NSLayoutConstraint(item: bannerView,
                                                attribute: .trailing,
                                                relatedBy: .equal,
                                                toItem: view,
                                                attribute: .trailing,
                                                multiplier: 1,
                                                constant: 0))
          view.addConstraint(NSLayoutConstraint(item: bannerView,
                                                attribute: .bottom,
                                                relatedBy: .equal,
                                                toItem: bottomLayoutGuide,
                                                attribute: .top,
                                                multiplier: 1,
                                                constant: 0))
        }
    

    【讨论】:

    • 这里使用的 3 个约束可以解决问题。我现在有一个全宽的广告横幅。只需切换 top 和 bottom 属性,使其显示在应用的顶部而不是底部。
    【解决方案3】:

    广告图片的宽度不同,因为您使用smart banner,根据图片宽度广告会进行调整。它在Admob Documentation 上得到了正确解释。

    智能横幅是一种广告单元,可在任何屏幕尺寸上以任一方向在不同设备上呈现屏幕宽度横幅广告。智能横幅通过“智能”检测设备当前方向的宽度并使广告视图具有该尺寸,从而帮助处理不同设备上日益增加的屏幕碎片。

    通常,手机上的智能横幅的纵向高度为 50dp,横向高度为 32dp。在平板电脑上,两个方向的高度通常为 90dp。 当图片广告不足以占据整个分配的空间时,图片将居中,两边的空间将被填充。

    【讨论】:

    • 是的,但图像既不居中。如果是的话,一切都会好的
    【解决方案4】:

    您在哪个设备上看到此广告?
    我的意思是谷歌有具体的指示 here 横幅尺寸,标准横幅为 320*50。
    因此,如果您在宽度为 320 的 iPhone 4s、iPhone 5 或 iPhone SE 上看到广告。所以它会全宽显示。但是,如果您在 iPhone 6 或 iPhone 6+ 等更大的设备中看到,那么您会在两侧看到边距。
    对于智能横幅, 他们还指定了here

    通常,手机上的智能横幅的纵向高度为 50dp 和 32dp 横向。在平板电脑上,高度通常为 90dp 方向。

    当图片广告的大小不足以占据整个分配的 空间,图像将居中,两侧的空间将 填写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2017-02-19
      相关资源
      最近更新 更多