简体   繁体   中英

AdBannerView in TableViewController is shown after TableView

I need to insert an AdBannerView to a TableViewController and position should be be bottom of the screen. I used this post as a guide which is written by Daniel Storm

The ad is shown but even there is only 1 cell in the tableview , I need to scroll down to be able to see it. Also I would like the ad to react to screen rotation.

AppDelegate.swift

import UIKit
import iAd // Import iAd

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate {

// Include the delegate for our banner

var window: UIWindow?
var adBannerView = ADBannerView() // Create our one ADBannerView

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Set delegate and hide banner initially
    adBannerView.delegate = self
    adBannerView.hidden = true
    return true
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    print("bannerViewDidLoadAd")
    adBannerView.hidden = false
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    print("bannerViewActionDidFinish")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    print("didFailToReceiveAdWithError: \(error)")
    adBannerView.hidden = true
}

ViewController.swift (tableviewcontroller in my case)

import UIKit

class ViewController: UIViewController {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // Create reference to our app delegate

override func viewWillAppear(animated: Bool) {
    // Position
    appDelegate.adBannerView.center = CGPoint(x: view.frame.midX,
        y: view.frame.height - appDelegate.adBannerView.frame.height / 2)
    // Add to view
    view.addSubview(appDelegate.adBannerView)
}

If all you want is a banner ad at the bottom of the screen, you can simply say something like

override func viewDidLoad() {
        super.viewDidLoad()
        self.canDisplayBannerAds = true
    }

And it will work automagically as they say.

I would recommend watching this video to understand the iAd API

I think you add your tableview after adding the bannerView in your VC, so the tableView covers the bannerView, you can adjust bannerView's zPosition to re-order them, add this line in your viewWillAppear :

adBannerView.layer.zPosition = 10

By the way,i think this process should be done in ViewDidLoad .

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