本文介绍了UBableViewController下面的ADBannerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UItableViewController中的tableView下面集成iAd横幅。目标是调整tableview的大小并添加到UIViewController的底部,在这种情况下是一个UITableViewController。我开始考虑adBannerView是一个UIView所以我编写了下面的代码和UIView,它工作,但当我尝试通过用ADBannerView替换它来发生同样的事情时,它不会发生。 ADBanner出现在正确的位置,但tableView调整大小丢失。

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.

有人可以尝试了解原因并帮助我找到更好的解决方案。没有使用footerView是否可行?

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

这里是代码。目前是Utils类中的静态方法。接下来我将在另一个上下文中使用它,但您应该可以轻松地自己测试它

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

    }

}


推荐答案

如果您需要的只是TableViewController底部的横幅,您可以使用预建行为,将 canDisplayBannerAds 设置为 true ,如下所示:

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
    }

}

这篇关于UBableViewController下面的ADBannerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 19:42