ADBannerView下面的UITableViewController

问题描述:

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

有人可以试着理解为什么,并帮助我找到更好的解决方案。没有使用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底部的旗帜,你可以只使用预置的行为,设置canDisplayBannerAdstrue这样:

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 
    } 

} 
+0

thanks。当1行代码取代时,真的很感激...呃,我不想指望他们...... – tylyo 2015-04-04 09:37:43