Place AdMob ad at the bottom of tableview controller - swift

删除回忆录丶 提交于 2019-12-23 06:58:08

问题


I've been trying to implement Admob on swift and everything is good, but i run out of problem because I'm using custom table view cell and "Table view controller" as the main swift file, so when i am dragging the BannerView into the storyboard which is the table view controller, the ad actually appear in the first cell of the table view ! instead i need to load it at the button of the tableview controller not inside the cells ?

@IBOutlet weak var bannerView: GADBannerView!


    override func viewDidLoad() {
        super.viewDidLoad()


        self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"

        self.bannerView.rootViewController = self

        var request: GADRequest = GADRequest()

        request.testDevices = [GAD_SIMULATOR_ID]

        self.bannerView.loadRequest(request)



        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

回答1:


One solution could be to create a new instance of bannerView instead of having it as an outlet and then add it as a subview to the tableView's superview.




回答2:


I had the same issue , i got it figured out by a slightly different method, But it works like a charm. So for all those Who are new to Swift and face this problem,

  1. If Your using a story board, copy all your views( ex labels, UIImage's ) from the prototype cell

  2. Go ahead and delete your View Controller. ( fear not , it will work eventually , trust me )

  3. Drag a blank ViewController and in the class , mention your class associated with earlier tableView.

  4. drag also tableview on it and selecting the tableview, put constraints form storyboard up & down-> 0 & left & right -> -20 .

  5. drag a tableview cell, and selecting that cell, in class mention the cell class you were using for the earlier Tableview cell, (tip-> You jus mite want to click on the module bellow the class and press enter, i got a stupid bug { unknown class in interface builder error , when i did it } )

  6. now you can just select the cell and paste your earlier views in it and create a new connection with the cell class @IB outlets.

  7. In the same Viewcontroller you can drag an empty UIView and make it a GADBannerView ( follow the Googles guide for admob iOS) Thats all in the IB

  8. Now go to your tableview class , the class extension just make it to UIViewController , and confirm to protocols UITableViewDataSource & UITableViewDelegate.

ex: class Foo: UIViewController, UITableViewDataSource, UITableViewDelegate {

  1. make a IB connection of your newly added UITableview in your main tableview (now viewController) class @IBOutlet weak var Footableview: UITableView!

Now jus add these two lines in the viewDidLoad Footableview.datasource = self Footableview.delegate = self

  1. Now just remove override form all tableview functions such as sections or RowForIndex functions, and wherever there is 'self.tableView' replace with 'self.Footableview'.

ScreenShot

  • Regards


来源:https://stackoverflow.com/questions/28797081/place-admob-ad-at-the-bottom-of-tableview-controller-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!