如何在横向模式下对 iAd 横幅进行编程。 Swift

How to program iAd banner in landscape mode. Swift

我想知道是否有人可以帮助我解决这个问题。我正在尝试在横向模式下创建一个 iAd 横幅。到目前为止,我使用了这段代码。它仍然显示高度和宽度与纵向模式相同的 iAd 横幅。虽然横幅居中。任何帮助将非常感激。

var adBannerView: ADBannerView!
let screenBounds: CGRect = UIScreen.mainScreen().bounds

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()
    print("height: \(screenBounds.height), width: \(screenBounds.width)")
}


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

你用Main.Storyboard吗?因为你可以在故事板上调整横幅视图的大小。

这对我有用:

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

}