Swift - 横幅加载时更改应用程序。 - xcode 6 GM

时间:2014-09-10 23:51:42

标签: swift height uitextview iad xcode6

我一直在研究一个主要基于文本的应用程序,底部有一个Iadbanner。

但是众所周知,Iads并不总是存在。所以我希望能够动态更新textView的高度,所以当隐藏横幅时,textView会占用浪费的空间。并在加载横幅时调整大小。

这是我目前对有问题的Viewcontroller的看法

import UIKit
import iAd

class DetailVC: UIViewController, UITextViewDelegate, ADBannerViewDelegate {

//Our label for displaying var "Items/cellName"
@IBOutlet var imageViewOutlet: UIImageView!

//connect in IB connection inspector with your ADBannerView
@IBOutlet var adBannerView: ADBannerView!

//Receiving variable assigned to our mainVC var "items"
var cellName: String = ""
var imageView: UIImageView = UIImageView()
var image = UIImage(named: "handcuffs.png")
var textViewText: String = ""
var textView: UITextView = UITextView(frame: CGRect(x: 5.0, y: 238.0, width: 315.00, height: 283.00))
//height = 332 for full screen 283 for small

override func viewDidLoad() {
    super.viewDidLoad()
    println("+--------------------+")
    println("| Detail view loaded |")
    println("+--------------------+")

    // Iad stuff
    self.adBannerView.delegate = self
    self.canDisplayBannerAds = true
    self.adBannerView.hidden = true //hide until ad loaded

    //Setting up the textView
    textView.text = textViewText
    textView.editable = false
    textView.backgroundColor = UIColor.clearColor()
    textView.font = UIFont(name: "Helvetica", size: 15)

    //adding textview as subview
    self.view.addSubview(textView)

    //ImageViewOutlets
    imageViewOutlet.image = image

    //Assign your string var to your navbar title
    self.title = cellName

func bannerViewWillLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewWillLoadAd")
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewDidLoadAd")
    //self.textView.removeFromSuperview()
    //textView = UITextView(frame: CGRect(x: 0.0, y: 238.0, width: 320.00, height: 283.00))
    //self.view.addSubview(textView)
    self.adBannerView.hidden = false //now show banner as ad is loaded
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    NSLog("bannerViewActionDidFinish")
    //optional resume paused app code
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    NSLog("bannerViewActionShouldBegin")
    //optional pause app code
    return true
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    NSLog("didFailToReceiveAdWithError")
}

//... your class implementation code
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

函数“bannerViewDidLoadAd”中注释掉的代码是我认为可以修复我的问题。可悲的是,这个功能似乎永远不会运行?我对Iads不是很熟悉所以希望有人可以给我一个关于如何在加载广告时更改textView高度的提示。

1 个答案:

答案 0 :(得分:1)

CanDisplayBannerAds:

  

设置此项以使用视图控制器启用横幅广告显示的自动管理。请注意,这将通过在视图控制器视图上方插入新的容器视图来修改视图控制器的视图层次结构,这一点非常重要。影响是视图控制器的视图属性将不再返回原始提供的视图,它将返回新容器。要访问原始视图,请使用originalContentView属性。

所以,删除这一行:

 self.canDisplayBannerAds = true

一切都应该有用。