使用代码更改约束常量

Change a constraints constant using code

我正在为我的所有视图创建此通用基本代码,它创建了一个横跨我所有页面的广告栏。我刚刚包含了此 的最佳答案中的一些代码,但我无法弄清楚为什么它不起作用。我正在努力做到这一点,如果 adBanner 没有加载我的标签,我会展开并占用 space。 如果这很明显,我深表歉意,但我对此并不陌生。 这是我的代码

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)

}


}

您在赋值时给出的是 == 而不是 =。

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)

}


}