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

How to stop an ad banner resizing the frame in Swift

我几乎在 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,则意味着根本不会显示任何广告。

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

这里有一些适用于你的工作代码,我刚刚工作过。这不需要 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 */
}