ADBannerView below UITableViewController

ⅰ亾dé卋堺 提交于 2019-12-06 12:49:45

问题


I want to integrate the iAd banner below a tableView in a UItableViewController. the goal is to resize the tableview and add to the bottom of the UIViewController, a UITableViewController in this case. I started considering that the adBannerView is a UIView so I wrote the code below and for a UIView, and it worked, but when I try to make same thing happen by replacing it with a ADBannerView it doesn't happen. The ADBanner appeared in the correct position but the tableView resizing is lost.

can somebody try to understand why and help me out to find a better solution. is it feasible without using the footerView?

here the code. At the moment is a static method within a Utils class. Next I'll use it in another context, but you should easily able to test it by yourself

class ViewControllerUtils {
    class func showBanner<C:UIViewController where C:ADBannerViewDelegate> ( viewController:C)  {

        println("*** showBanner isLandscape:\(UIDevice.currentDevice().orientation.isLandscape)")

        // you don't care about it for the moment.
        var bannerHeight:CGFloat = 50.0
        if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad{
            bannerHeight = 66.0
        } else if UIDevice.currentDevice().orientation.isLandscape {
            bannerHeight = 32.0
        }
        println("bannerHeight: \(bannerHeight)")

        // created a local variable in order to update the original frame
        var viewFrame  = viewController.view.frame

        UIView.animateWithDuration(1.0, animations: { () -> Void in
                        println("viewFrame \(viewFrame)")

            viewFrame.size.height -= bannerHeight
            viewController.view.frame = viewFrame
            println("viewFrame \(viewFrame)")

            }) { (ended:Bool) -> Void in

                var x = CGPoint(x: viewFrame.origin.x, y: viewFrame.origin.y + viewFrame.size.height)
                var bannerFrame = CGRect(origin:  x, size: CGSize(width: viewFrame.size.width, height: bannerHeight))

                var container = UIView(frame: bannerFrame)
                container.backgroundColor = UIColor.redColor()

                //without this line it works like expected.
                //with it tableview resizing is not applied anymore
                container.addSubview(ADBannerView(frame: CGRect(origin:  CGPointZero, size: CGSize(width: viewFrame.size.width, height: bannerHeight))))

                viewController.view.superview?.addSubview(container)
        }

    }

}

回答1:


If all you need is the banner at the bottom of the TableViewController, you can just use the prebuilt behavior, setting canDisplayBannerAds to true like this:

import UIKit
import iAd

class MainViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.canDisplayBannerAds = true
    }

}


来源:https://stackoverflow.com/questions/29443149/adbannerview-below-uitableviewcontroller

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