我很确定代码是正确的,但由于某些原因,当广告打开时,它不会出现在应用程序的顶部。我在swift中为我的IOS应用程序做这项工作,希望当你第一次打开应用程序时,广告出现在“主屏幕”上。

import UIKit

import GoogleMobileAds

class ViewController: UIViewController, GADBannerViewDelegate {

    @IBOutlet var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        self.view.addSubview(bannerView)
       bannerView.adUnitID = "ca-app-pub-1912006279271616/2057666171"
       bannerView.rootViewController = self
       bannerView.load(GADRequest())
        let requestAd: GADRequest = GADRequest()
        requestAd.testDevices = [kGADSimulatorID]
        bannerView.load(requestAd)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

最佳答案

下面的代码对我有效-我看到的与您的代码的唯一区别是设置frame.origin属性。也许它默认为(0,0),如果视图中有导航栏,它可能隐藏在导航栏下。试着把横幅放在屏幕的下方并检查。

let smart = kGADAdSizeSmartBannerPortrait
let banner = GADBannerView(adSize: smart)
banner.frame.origin = CGPoint(x: 0, y: offsetY) // set your own offset
banner.adUnitID = "ca-app-pub-........." // insert your own unit ID
banner.rootViewController = self
self.view.addSubview(banner)
let request = GADRequest()
banner.load(request)

09-25 19:32