Miles H.
Miles H.

Reputation: 285

How to add iAd banner programmatically in landscape in sprite kit swift

Does anyone know how to add an iAd banner programmatically? I am making a game using Sprite Kit and Swift. The game is in Landscape mode. Any help would be much appreciated.

I'm using SKScene's, not ViewControllers fyi.

I also tried this code :

class GameViewController: UIViewController, ADBannerViewDelegate {

var adBannerView: ADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Presenting scene without using GameScene.sks
    let skView = self.view as SKView
    let myScene = Menu(size: skView.frame.size)
    skView.presentScene(myScene)

    loadAds()

}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    println("Leaving app to the Ad")

    return true
}

func bannerViewDidLoadAd(banner: ADBannerView!) {

    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
    adBannerView.frame = CGRectOffset(adBannerView.frame,0.0,0.0)
    adBannerView.hidden = false


    println("Displaying the Ad")
}


func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {

    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height + view.bounds.size.height)
    println("Ad is not available")
}


func loadAds() {
    adBannerView = ADBannerView(frame: CGRect.zeroRect)
    adBannerView.frame = CGRectOffset(adBannerView.frame,0,0.0)
    adBannerView.delegate = self
    adBannerView.hidden = true
    view.addSubview(adBannerView)
}

This presents the add on the bottom of the screen but with the height and width as if it was in portrait mode, I need the add to be shown using the full width of the landscape mode and less heigh.

Upvotes: 0

Views: 1446

Answers (2)

Alvaro
Alvaro

Reputation: 526

This worked for me

    var UIiAd: ADBannerView = ADBannerView()

    override func viewWillAppear(animated: Bool) {

    UIiAd.setTranslatesAutoresizingMaskIntoConstraints(false)
    UIiAd.delegate = self
    self.view.addSubview(UIiAd)
    let viewsDictionary = ["bannerView":UIiAd]
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))

    }

Upvotes: 0

LinusG.
LinusG.

Reputation: 28912

This code might solve your question:

let screenBounds: CGRect = UIScreen.mainScreen().bounds

var adBannerView: ADBannerView  
adBannerView = ADBannerView(frame: CGRectMake(0, 0, 50, screenBounds.width))
adBannerView.center = CGPoint(x: screenBounds.width/2, y: screenBounds.height-adBannerView.frame.height)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)

Hope it helps :)

Upvotes: 2

Related Questions