如何在Swift中停止调整框架大小的广告横幅

时间:2015-08-28 09:08:14

标签: ios swift iad banner adbannerview

我在Swift中几乎完成了IOS游戏,我的横幅出现了问题。我设法找到一些代码来阻止它调整视图框架的大小,但现在它似乎完全随机调整大小;有时不调整大小(根据需要)有时会调整大小,默认情况下。

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
}

}

我已经阅读过与canDisplayBannerAds设置有关的地方,但如果我将其更改为false,则表示根本没有显示任何广告。

我希望明天发布,并且真的希望在发布时修复此问题:)

1 个答案:

答案 0 :(得分:3)

以下是一些适用于我的工作代码。这不需要self.candisplaybannerads = true,因为我遇到了一些问题。广告会根据屏幕尺寸自动更改尺寸,并位于屏幕底部。在我的spritkit游戏中它没有调整场景。

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 */
}