gpt4 book ai didi

ios - 添加 ADBannerView 后, Controller 不会调整 View 大小

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:23 24 4
gpt4 key购买 nike

通读 Apple 关于 iAD 的文档,我得到的印象是,如果我将 ADBannerView 添加到我的 Controller 的 View 树中 - 其他 View 将被缩放以适应添加。但显然情况并非如此,正如您在此处看到的那样:

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

我错过了什么吗?如果我将设置 Controller 的 self.canDisplayBannerAds = true,则横幅将正确显示在底部,其余部分按我预期的方式缩放。

enter image description here

是不是因为我使用的是 presentScene,它添加了 SKScene 而不是 View ?

更新

我试过添加约束,但我想我前面有很多阅读资料,因为我还没有弄明白 :) 我在谷歌上搜索了一段时间并想出了这个,但这并没有改变任何东西所以可能我做错了(将其添加到 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))

最佳答案

您可以将完整代码替换为

   self.canDisplayBannerAds = true

这是一个简短的tutorial .

关于ios - 添加 ADBannerView 后, Controller 不会调整 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com