sasklacz
sasklacz

Reputation: 3628

Controller does not resize the views when ADBannerView added

Reading through Apple's documentation on iAD I got the impression, that if I'll add the ADBannerView to my controller's view tree - other views will be scaled to fit the add. But apparently that's not the case as you can see here :

enter image description here

That's the code I'm using :

import UIKit
import SpriteKit
import iAd

class GameViewController: UIViewController, ADBannerViewDelegate {

    var mainView: SKView!
    let adBanner = ADBannerView(adType: .Banner)

    override func viewDidLoad() {
        super.viewDidLoad()

        mainView = self.view as? SKView
        mainView.showsDrawCount = true
        mainView.showsNodeCount = true
        mainView.showsFPS = true

        adBanner.delegate = self
        adBanner.center = CGPoint(x: adBanner.center.x, y: mainView.bounds.size.height - adBanner.frame.size.height / 2)
        adBanner.hidden = true
    }

    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("error: \(error)")
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {
        print("WILL LOAD BANNER")
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        print("Loaded Ad \(banner)")
        adBanner.hidden = false
    }

    override func viewWillAppear(animated: Bool) {
        let helloScene = HelloScene(size: mainView.frame.size)
        mainView.presentScene(helloScene)
        mainView.addSubview(adBanner)
    }

    override func shouldAutorotate() -> Bool {
        return false
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return UIInterfaceOrientationMask.AllButUpsideDown
        } else {
            return UIInterfaceOrientationMask.All
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

Am I missing something ? If I'll set controller's self.canDisplayBannerAds = true then banner is displayed properly at the bottom with the rest scaled as I would expect.

enter image description here

Is it because I'm using presentScene, which adds SKScene and not a view ?

UPDATE

I've tried adding constraints, but I guess there's a lot of reading ahead of me cause I couldn't yet figure that out :) I googled for a while and came up with this, but that didn't change anything so probably I'm doing it wrong (added that to viewDidLoad :

mainView.addSubview(adBanner)
let viewsDictionary = ["adBanner": adBanner]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))

Upvotes: 1

Views: 72

Answers (1)

Stefan
Stefan

Reputation: 5451

You can replace your complete code with

   self.canDisplayBannerAds = true

Here is a short tutorial.

Upvotes: 2

Related Questions