我想在UItableViewController中集成tableView下面的iAd横幅。目标是调整表视图的大小,并在UIViewController的底部添加一个UITableViewController。我开始考虑adBannerView是一个UIView,所以我在下面为UIView编写了代码,它起了作用,但当我试图用adBannerView替换它来实现同样的事情时,它并没有发生。ADBanner出现在正确的位置,但tableView大小调整丢失。
有人能不能试着理解为什么,帮我找到一个更好的解决方案。不使用footerView可行吗?
这是代码。目前是Utils类中的一个静态方法。接下来我将在另一个上下文中使用它,但您应该可以很容易地通过自己测试它
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
,如下所示:
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
}
}