简体   繁体   中英

XCODE / SWIFT - reposition ADMOB banner to the bottom

i am having trouble reposition my admob banner at the bottom of the screen ,no matter what i have tried it stay at the top .. i found out alot of this kind of questions, i have tried what they did and nothing work. Here is what im trying :

 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)

This is how i do it:

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

The GoogleAds GitHub has an 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)

After the normal setup then add the constraints. This got me what I wanted positioning the banner ad to the bottom center.

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM