简体   繁体   中英

ADBannerView below UITableViewController

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

    }

}

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
    }

}

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