如何在sprite kit swift中以编程方式添加iAd横幅

时间:2015-03-24 13:38:46

标签: ios swift iad skscene

有谁知道如何以编程方式添加iAd横幅?我正在使用Sprite Kit和Swift制作游戏。游戏处于横向模式。任何帮助将非常感激。

我使用的是SKScene,而不是ViewControllers fyi。

我也试过这段代码:

class GameViewController: UIViewController, ADBannerViewDelegate {

var adBannerView: ADBannerView!

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

}

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: CGRect.zeroRect)
    adBannerView.frame = CGRectOffset(adBannerView.frame,0,0.0)
    adBannerView.delegate = self
    adBannerView.hidden = true
    view.addSubview(adBannerView)
}

这会在屏幕底部显示添加,但高度和宽度就像在纵向模式下一样,我需要使用横向模式的整个宽度显示添加并减少高度。

2 个答案:

答案 0 :(得分:2)

此代码可能会解决您的问题:

let screenBounds: CGRect = UIScreen.mainScreen().bounds

var adBannerView: ADBannerView  
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)

希望有所帮助:)

答案 1 :(得分:0)

这对我有用

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

    }