0
votes

I was wondering if someone can help me with this. I am trying to create a iAd banner in landscape mode. So far I used this code. It still shows the iAd banner with the same height and width as if it was displayed in a portrait mode. The banner is centered though. Any help would be much appreciated.

var adBannerView: ADBannerView!
let screenBounds: CGRect = UIScreen.mainScreen().bounds

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()
    print("height: \(screenBounds.height), width: \(screenBounds.width)")
}


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: 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)
}
2

2 Answers

0
votes

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))

}
0
votes

Do you use the Main.Storyboard? Because you can just adjust the size of the banner view on the storyboard.