使用代码更改约束常量

时间:2015-05-21 04:59:27

标签: ios swift cocoa-touch constraints

我正在为我的所有视图创建此通用基本代码,它会创建一个横跨我所有网页的广告栏。我刚刚提供了一些代码来自Question的最佳答案,我无法解释为什么它不起作用。我试图这样做,如果adBanner没有加载我的标签伸展并占用该空间。 如果这很明显,我很抱歉,但我是新手。 这是我的代码

import Foundation
import UIKit
import iAd


class dayPicker: UIViewController  , ADBannerViewDelegate{

var UIiAd: ADBannerView = ADBannerView()
var SH = UIScreen.mainScreen().bounds.height
var AH = CGFloat()
@IBOutlet var constOne: NSLayoutConstraint!
@IBOutlet var constTwo: NSLayoutConstraint!

func appdelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

override func viewWillDisappear(animated: Bool) {
    UIiAd.delegate = nil
    UIiAd.removeFromSuperview()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 1
    AH = 50
    UIView.commitAnimations()
    self.constOne.constant == 58
    self.constTwo.constant == 58
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 0
    AH = 0
    UIView.commitAnimations()
    self.constOne.constant == 8
    self.constTwo.constant == 8


}
override func viewWillAppear(animated: Bool) {

    UIiAd.delegate = self
    UIiAd = self.appdelegate().UIiAd
    UIiAd.frame = CGRectMake(0, SH - AH , 0, 0)
    self.view.addSubview(UIiAd)

}


}

1 个答案:

答案 0 :(得分:2)

您在分配时给予==而不是=。

import Foundation
import UIKit
import iAd

class dayPicker: UIViewController  , ADBannerViewDelegate{

var UIiAd: ADBannerView = ADBannerView()
var SH = UIScreen.mainScreen().bounds.height
var AH = CGFloat()
@IBOutlet var constOne: NSLayoutConstraint!
@IBOutlet var constTwo: NSLayoutConstraint!

func appdelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

override func viewWillDisappear(animated: Bool) {
    UIiAd.delegate = nil
    UIiAd.removeFromSuperview()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 1
    AH = 50
    UIView.commitAnimations()
    self.constOne.constant = 58
    self.constTwo.constant = 58
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 0
    AH = 0
    UIView.commitAnimations()
    self.constOne.constant = 8
    self.constTwo.constant = 8


}
override func viewWillAppear(animated: Bool) {

    UIiAd.delegate = self
    UIiAd = self.appdelegate().UIiAd
    UIiAd.frame = CGRectMake(0, SH - AH , 0, 0)
    self.view.addSubview(UIiAd)

}


}