XCODE / SWIFT - 将ADMOB横幅重新定位到底部

时间:2014-11-03 16:07:20

标签: ios xcode swift position admob

我无法在屏幕底部重新定位我的admob横幅 ,无论我尝试过什么留在顶部 ..我发现了很多这样的问题,我已经尝试过他们做了什么,没有任何效果。 以下是我的尝试:

 bannerView = GADBannerView(adSize: kGADAdSizeBanner)
            bannerView?.adUnitID = "ca-app-pub-466066799532826501/8972705271"
            bannerView?.delegate = self
            bannerView?.rootViewController = self



            let screenRect = UIScreen.mainScreen().bounds

            let screenWidth = screenRect.size.width
            let screenHeight = screenRect.size.height
            let screenXPos = (screenWidth / 2)
            let screenYPos = screenHeight - kGADAdSizeBanner.size.height
            bannerView!.frame = CGRectMake(screenWidth/2.0 - bannerView!.frame.size.width/2.0, screenHeight - bannerView!.frame.size.height,
                bannerView!.frame.size.width,bannerView!.frame.size.height);



            self.view.addSubview(bannerView!)
            bannerView?.loadRequest(GADRequest())

            timerAd?.invalidate()
            timerAd = NSTimer.scheduledTimerWithTimeInterval(40, target: self, selector: "GoogleAdRequestTimer", userInfo: nil, repeats: true)

3 个答案:

答案 0 :(得分:9)

我就是这样做的:

func adViewDidReceiveAd(view: GADBannerView!) {
    println("adViewDidReceiveAd:\(view)");
    bannerDisplayed = true
    relayoutViews()
}


func relayoutViews() {
    if (bannerDisplayed) {
        var bannerFrame = bannerView!.frame
        bannerFrame.origin.x = 0
        bannerFrame.origin.y = screenHeight - bannerFrame.size.height

        bannerView!.frame = bannerFrame
    // and so on

答案 1 :(得分:0)

GoogleAds GitHub有example

 bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
 bannerView.adUnitID = "*******YOUR CA-PUB-ID*******"
 bannerView.rootViewController = self

 let request = GADRequest()
 request.testDevices = [ kGADSimulatorID, "********* YOUR SIMULATOR ID*******" ]

 self.view.addSubview(bannerView)
 bannerView.translatesAutoresizingMaskIntoConstraints = false

 view.addConstraint(NSLayoutConstraint(item: bannerView, attribute: .bottom,
     relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0))
 view.addConstraint(NSLayoutConstraint(item: bannerView, attribute: .centerX,
     relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0))

 bannerView.load(request)

答案 2 :(得分:0)

在正常设置之后,请添加约束。 这就是我想要的将横幅广告定位在底部中心的地方。

func addBannerViewToView(_ bannerView: GADBannerView) {
     bannerView.translatesAutoresizingMaskIntoConstraints = false
     view.addSubview(bannerView)
     view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: view.safeAreaLayoutGuide,
                            attribute: .bottom,
                            multiplier: 1,
                            constant: 0),
        NSLayoutConstraint(item: bannerView,
        attribute: .centerX,
        relatedBy: .equal,
        toItem: view.safeAreaLayoutGuide,
        attribute: .centerX,
        multiplier: 1,
        constant: 0)]
        )

    }