![](/img/trans.png)
[英]Dismissing UITableViewController changes ADBannerView location
[英]ADBannerView below UITableViewController
我想将iAd横幅广告整合到UItableViewController中的tableView下。 目标是调整表视图的大小并将其添加到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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.