添加ADBannerView时,Controller不会调整视图大小

时间:2015-11-11 06:47:45

标签: ios swift sprite-kit iad adbannerview

阅读Apple关于iAD的文档我得到的印象是,如果我将ADBannerView添加到我的控制器的视图树中,其他视图将被缩放以适应添加。但显然情况并非如你所见:

enter image description here

我正在使用的代码:

import UIKit
import SpriteKit
import iAd

class GameViewController: UIViewController, ADBannerViewDelegate {

    var mainView: SKView!
    let adBanner = ADBannerView(adType: .Banner)

    override func viewDidLoad() {
        super.viewDidLoad()

        mainView = self.view as? SKView
        mainView.showsDrawCount = true
        mainView.showsNodeCount = true
        mainView.showsFPS = true

        adBanner.delegate = self
        adBanner.center = CGPoint(x: adBanner.center.x, y: mainView.bounds.size.height - adBanner.frame.size.height / 2)
        adBanner.hidden = true
    }

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

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("error: \(error)")
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {
        print("WILL LOAD BANNER")
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        print("Loaded Ad \(banner)")
        adBanner.hidden = false
    }

    override func viewWillAppear(animated: Bool) {
        let helloScene = HelloScene(size: mainView.frame.size)
        mainView.presentScene(helloScene)
        mainView.addSubview(adBanner)
    }

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

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

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

我错过了什么吗?如果我设置了控制器self.canDisplayBannerAds = true,则横幅会在底部正确显示,其余部分会按照我的预期进行缩放。

enter image description here

是因为我使用的是presentScene,它会添加SKScene而不是视图吗?

更新

我已尝试添加约束,但我想在我前面有很多阅读因为我无法解决这个问题:)我用谷歌搜索了一段时间并想出了这个,但这并没有改变任何事情,所以我可能做错了(将其添加到viewDidLoad

mainView.addSubview(adBanner)
let viewsDictionary = ["adBanner": adBanner]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))

1 个答案:

答案 0 :(得分:2)

您可以使用

替换完整的代码
   self.canDisplayBannerAds = true

这是一个简短的tutorial