M. Qasem
M. Qasem

Reputation: 37

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

I have been wanting to properly fix an iAd to the top of my screen and I tried several solutions but to no avail. I need to fix the issue where you get a white rectangle in place of the iAd banner when disconnected and also it sometimes appears on the top and sometimes on the bottom. How can I make it always appear in the top centre, instead of going from top to bottom and bottom to top randomly?

My code:

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

Upvotes: 1

Views: 278

Answers (1)

Daniel Storm
Daniel Storm

Reputation: 18878

The ADBannerView displaying on the bottom of your devices screen is created by self.canDisplayBannerAds = true. self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAd banners in your application. This will create an ADBannerView for you and show or hide the ADBannerView depending on whether it receives an ad or not from the iAd network.

You need to remove self.canDisplayBannerAds = true from your viewDidLoad.

Upvotes: 1

Related Questions