iOS: Admob 智能横幅不占满宽度

iOS: Admob smart banner does not taking full width

我创建了横幅:

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

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

如何让它全宽?

广告图片的宽度不同,因为您使用的是 smart banner 根据图片宽度广告将进行调整。它在 Admob Documentation.

上得到了正确解释

智能横幅广告单元可以在不同设备的任何屏幕尺寸上以任一方向呈现屏幕宽度的横幅广告。智能横幅通过 "smartly" 检测设备在其当前方向的宽度并使广告视图达到该尺寸,帮助解决不同设备之间日益增加的屏幕碎片问题。

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

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

Typically, Smart Banners on phones have a height of 50dp in portrait and 32dp in landscape. On tablets, height is normally 90dp in both orientations.

When an image ad isn't large enough to take up the entire allotted space, the image will be centered, and the space on either side will be filled in.

Typically, Smart Banners on phones have a height of 50dp in portrait and 32dp in landscape. On tablets, height is normally 90dp in both orientations.

When an image ad isn't large enough to take up the entire allotted space, the image will be centered, and the space on either side will be filled in.

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

 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))
    }