如何以编程方式将 iAd 横幅添加到屏幕的顶部中央?

How can I add an iAd banner programmatically to the top centre of the screen?

我一直想将 iAd 正确固定到屏幕顶部,我尝试了多种解决方案但无济于事。我需要解决以下问题:当断开连接时,iAd 横幅会出现一个白色矩形,有时它会出现在顶部,有时会出现在底部。我怎样才能让它总是出现在顶部中心,而不是随机地从上到下和从下到上?

我的代码:

class ViewController: UIViewController, ADBannerViewDelegate, UITextFieldDelegate  {
    @IBOutlet var adBannerView: ADBannerView!
    var bannerIsVisible : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        self.canDisplayBannerAds = true
        self.adBannerView?.delegate = self
        self.adBannerView?.hidden = true
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        self.adBannerView?.hidden = true
    }
    func bannerViewActionDidFinish(banner: ADBannerView!) {

    }
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        self.adBannerView?.hidden = false
    }
    func bannerViewWillLoadAd(banner: ADBannerView!) {
    }

显示在设备屏幕底部的 ADBannerView 是由 self.canDisplayBannerAds = true 创建的。 self.canDisplayBannerAds = true 可用于 轻松 在您的应用程序中实施 iAd 横幅的方式。这将为您创建一个 ADBannerView,并根据它是否从 iAd 网络接收到广告来显示或隐藏 ADBannerView

您需要从 viewDidLoad 中删除 self.canDisplayBannerAds = true