Search code examples
iosswiftiadbanneradbannerview

How to stop an ad banner resizing the frame in Swift


I have almost completed a game for IOS in Swift and am having an issue with my banner. I managed to find some code to stop it resizing the view frame, but now it seems to resize completely randomly; sometimes not resizing (as desired) sometimes resizing as is default.

extension SKNode {
class func unarchiveFromFile(file : String) -> SKNode? {

    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

class GameViewController: UIViewController, ADBannerViewDelegate {

let gameCenterPlayer = GKLocalPlayer.localPlayer()

var bannerView:ADBannerView?

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNoAds:", name: "noAdsID", object: nil)

        skView.presentScene(scene)

    }

// disabling ads if no-ads is purchased

    if bannerAdsOff != true {
        self.canDisplayBannerAds = true
    } else {
        self.canDisplayBannerAds = false
    }

    bannerView = ADBannerView()
    bannerView?.delegate = self
    bannerView?.frame = CGRectZero
    bannerView?.backgroundColor = UIColor.clearColor()

    bannerView!.frame = CGRectMake(0, self.view.frame.size.height - self.bannerView!.frame.size.height, self.bannerView!.frame.size.width, self.bannerView!.frame.size.height)
    bannerView?.hidden = true

    view.addSubview(bannerView!)

}


func updateNoAds(notification: NSNotification) {

    self.canDisplayBannerAds = false

    println("turning can Display banner ads off")

    self.bannerView?.hidden = true

    println("bannerview hidden = \(self.bannerView?.hidden)")

}

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

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

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

func bannerViewDidLoadAd(banner: ADBannerView!) {

    if bannerAdsOff != true {

    self.bannerView?.hidden = false

    }
}

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

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

}

I have read somewhere that it has to do with the canDisplayBannerAds setting, but if I change that to false, it just means no ads are shown at all.

I'm hoping to release tomorrow and would really like to have this fixed for the release :)


Solution

  • Here is some working code for you that just worked for me. This does not need self.candisplaybannerads = true as I had some issues with that. The ad automatically changes the size according to the screen size and is located at the bottom of the screen. In my spritkit game it did not resize the scene.

    import iAd
    
    class viewController: UIViewController, ADBannerViewDelegate {
    
    var AdBanner = ADBannerView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
            /* Ad Banner Settings */
    
            AdBanner = ADBannerView()
            AdBanner.frame = CGRectZero
            AdBanner.delegate = self
            self.AdBanner.frame = CGRectMake(0, self.view.frame.size.height-self.AdBanner.frame.size.height, self.AdBanner.frame.size.width, self.AdBanner.frame.size.height)
            AdBanner.backgroundColor = UIColor.clearColor()
            self.view .addSubview(AdBanner)
    
    }
    
    /* All iAd Functions */
    
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    /* whatever you need */
        return true
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!) {
    /* whatever you need */
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        AdBanner.hidden = false
    }
    
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("Error Loading Ad")
    /* whatever you need */
        AdBanner.hidden = true
    }
    func bannerViewWillLoadAd(banner: ADBannerView!) {
    /* whatever you need */
    }